在具有两个模块的应用程序中:GUI.py 和 calcs.py,其中 GUI 导入和使用来自 calcs 的函数,那么 calcs 函数更新 GUI 中的进度条的好方法是什么?
当我将所有代码都放在一个模块中时,它曾经很简单。我将它重构为两个(仍在学习......),这是我现在唯一无法解决的问题。
例如,作为一个非常简单的示例,GUI.py 模块具有:
import tkinter as tk
import tkinter.ttk as ttk
import calc as c
class GUI(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.prog = tk.DoubleVar()
self.result = tk.StringVar()
self.label = ttk.Label(textvariable = self.result)
self.progbar = ttk.Progressbar(self, maximum = 10, variable = self.prog)
self.button= ttk.Button(self, text = 'Go', command = lambda: self.result.set(c.stuff()))
self.label.pack()
self.progbar.pack()
self.button.pack()
a = GUI()
a.mainloop()
和 calc.py 具有:
def stuff():
counter = 0
for i in range(1, 11, 1):
counter += 1
# find a way to pass the value of counter to the GUI progress bar
# do lots of stuff that takes quite some time
return 'hey, a result!'
将 calc 函数中的进度计数器与 GUI 中的进度条变量链接的好方法是什么?
当他们一起在一个模块中时,这当然很简单 - 可以调用
prog.set(counter)
a.update_idletasks()
但不再。谷歌搜索并阅读有关此内容的信息,我尝试使其成为线程并使用队列来链接它们,但这 a) 似乎有点矫枉过正,b) 很困难......我没有让它工作......