从调用的函数更新 Tkinter 进度条(通过单击按钮),函数在单独的文档中

Update Tkinter progressbar from function called (by button click), function is in separate document

我正在尝试更新 tkinter 中的进度条。 首先我有两个文件。 文档 1:conatins GUI 文档 2:包含在按下附加到 GUI 的按钮小部件时调用的函数。

当我在文档 2 中执行计算时,我想更新文档 1 中的进度条。 代码示例如下。 (顺便提一下,这是我在 python 中的第一个脚本,所以如果我的任何编码有误,请纠正我)

#Document 1 
from Document2 import *

import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
from tkinter import font as tkfont # python 3
from PIL import ImageTk, Image
from tkinter.ttk import Progressbar




class TEST(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.resizable(self, width=False, height=False)
        tk.Tk.title(self, "APP NAME")
        tk.Tk.iconbitmap(self, filepath_NAME_icon)
        tk.Tk.geometry(self, "465x262")

        #self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (ProjectingPage):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("ProjectingPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()




class ProjectingPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, bg=bg_color)
        self.controller = controller


        Button_get_TK = Button(self, text="Indsæt terrænkote",
                               font=("Calibri", btn_font_size, "normal"),
                               width=std_button_width,
                               bg=bg_color_button,
                               fg=fg_color_button,
                               borderwidth=size_border_width,
                               activebackground=act_bg,
                               command=Get_TK)
        Button_get_TK.place(x=12, y=166)
        Button_get_TK.update()

        self.progress = Progressbar(self, orient = HORIZONTAL,
              length = 100, mode = 'determinate')
        self.progress.pack()

    def UpdateProgressBar(self, data):
        self.progress.start()
        self.progress.update_idletasks()

#Document 2
import Document1 as KTP
from Document1 import *

def Get_TK():
    for i in range(0,100):
        test_progressbar = KTP.ProjectingPage
        test_progressbar.UpdateProgressBar(i)

我已将我的代码精简到只包含相关部分。我需要了解通过从单独的文档调用函数来更新进度条的原理。

我真的希望有人能帮助我。提前致谢:-)

我不会将 Document1 中的元素用作 Document2 中的硬编码,而是用作函数中的参数。

#Document 2
# without imports from Document1

def Get_TK(item):
    test_progressbar = item.ProjectingPage
    for i in range(100):
        test_progressbar.UpdateProgressBar(i)

然后我会使用 lambda 来赋值函数

command=lambda:Get_TK(self.UpdateProgressBar):

我会说 send as argument 符合The Zen of Python

的原则
Explicit is better than implicit.

但是如果我必须使用 ProjectingPage 中的 Get_TK 来访问同一个 class ProjectingPage 中的元素,那么我不会将它放在单独的文件中,但是直接在 ProjectingPage

class ProjectingPage(tk.Frame):

    def Get_TK(self):
        for i in range(100):
            self.UpdateProgressBar(i)