简体   繁体   English

Tkinter顶级小部件未显示-python

[英]Tkinter Toplevel widgets not displaying - python

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. 我正在使用python Tkinter中的顶级窗口工作,在其他代码完成之前,似乎无法显示嵌入式小部件。 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. 见下文。

Here is my Toplevel code: 这是我的顶级代码:

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()

You have to know that tkinter is single threaded. 您必须知道tkinter是单线程的。 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. 同样,窗口(以及您在屏幕上看到的所有窗口)仅在空闲(不执行任何操作)或调用w.update_idletasks()时更新其外观,其中w是任何小部件。 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()

Based upon the @Eric Levieil's link above, it was as simple as adding this to my code: 基于上面的@Eric Levieil的链接,就像将其添加到我的代码中一样简单:

self.progress.progress_frame.update()

Full change: 完整更改:

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.update()
        self.progress.progress_frame.destroy()

Thank you Eric! 谢谢你,埃里克!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM