Tkinter Toplevel 小部件不显示 - python
我正在使用 python Tkinter 中的 Toplevel 窗口,在我的其他代码完成之前,我似乎无法显示嵌入的小部件.框架出现了,它正确地循环了我的其他代码,但文本/进度条小部件只有在我以某种方式中断循环时才会出现.帧在最后被成功销毁.见下文.
I am working with a Toplevel window in python Tkinter and I cannot seem to get the embedded widgets to show up until my other code has completed. The frame shows up, it loops through my other code properly, but the text/progressbar widget only show up if I somehow interrupt the loop. The frame is successfully destroyed at the end. See below.
这是我的顶级代码:
class ProgressTrack:
def __init__(self, master, variable, steps, application):
self.progress_frame = Toplevel(master)
self.progress_frame.geometry("500x140+30+30")
self.progress_frame.wm_title("Please wait...")
self.progress_frame.wm_iconbitmap(bitmap="R:\\CHPcomm\\SLS\\PSSR\\bin\\installation_files\\icon\\PSIDiaryLite.ico")
progress_text = Canvas(self.progress_frame)
progress_text.place(x=20,y=20,width=480,height=60)
progress_text.create_text(10, 30, anchor=W, width=460, font=("Arial", 12), text="Please do not use " + application + " during execution. Doing so, will interrupt execution.")
self.progress_bar = Progressbar(self.progress_frame, orient='horizontal', length=440, maximum=steps, variable=variable, mode='determinate')
self.progress_bar.place(x=20,y=100,width=450,height=20)
我从以下类的实例中调用它,该类在用户单击主窗口上的按钮时创建:
And I call it from an instance of the following class which is created when the user clicks a button on the master window:
class Checklist:
def __init__(self, master, var):
self.progress = ProgressTrack(master, 0, 5, 'Microsoft Word')
while var:
#MY OTHER CODE
self.progress.progress_bar.step()
self.progress.progress_frame.destroy()
你必须知道 tkinter 是单线程的.此外,窗口(以及您在屏幕上看到的所有内容)仅在空闲(什么都不做)或您调用 w.update_idletasks()
时更新其外观,其中 w
是任何小部件.这意味着当您在循环中更改进度条时,在循环完成之前屏幕上不会发生任何事情.
You have to know that tkinter is single threaded. Also the window (and all you see on the screen) updates its appearance only when idle (doing nothing) or when you call w.update_idletasks()
where w
is any widget. This means when you are in a loop, changing a progress bar, nothing will happen on the screen until the loop is finished.
所以你的新代码现在可以是
So your new code could now be
while var:
#MY OTHER CODE
self.progress.progress_bar.step()
self.progress.progress_frame.update_idletasks()
self.progress.progress_frame.destroy()