Tkinter Toplevel widgets not displaying - python

我的未来我决定 提交于 2019-12-13 02:19:56

问题


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.

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

回答1:


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



回答2:


Based upon the @Eric Levieil's link above, it was as simple as adding this to my code:

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!



来源:https://stackoverflow.com/questions/29634742/tkinter-toplevel-widgets-not-displaying-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!