Python tkinter 弹出窗口未在子线程之前打开

Python tkinter popup not opening prior to subthread

我试图在此处打开一个显示进度条的弹出窗口,但我 运行 遇到了一个问题,即弹出窗口在线程完成后才打开(线程启动 调用弹出窗口后)。

此外,我 可以 通过在 self.bar 上调用 .pack() 来让弹出窗口在线程启动之前显示,因为这个错误如下所示:(我如果我调用 .pack.grid 调用分开,则会得到相同的错误)

File "\hinter\ui\progress.py", line 26, in __init__
    self.bar.grid(row=2, column=0).pack()
AttributeError: 'NoneType' object has no attribute 'pack'

这是我排列所有内容的代码:

progress_popup = hinter.ui.progress.Progress(0, 'Downloading and processing: Champions') # Should open popup
the_thread = threading.Thread(
    target=self.load_champions,
    args=(refresh,)
)
the_thread.start() # Run the download code in another thread
the_thread.join() # Wait to finish, so downloads can be completed one at a time for now
progress_popup.update(20, 'next') # Update the popup

此代码是 Progress.__init__ 的主体,弹出窗口应在此处打开:

self.popup = Toplevel()

Label(self.popup, text='Downloading Game data ...').grid(row=0, column=0, padx=20)
self.status_text = Label(self.popup, text=current_status)
self.status_text.grid(row=3, column=0, padx=20)

self.bar = Progressbar(self.popup, orient=HORIZONTAL, length=100, mode='determinate')
self.bar['value'] = self.current_percentage
self.bar.grid(row=2, column=0)

我只是不明白为什么像...这样的弹出窗口需要这么长时间才能打开,或者什么的,除非我让它出错,在这种情况下它会立即打开。

我不知道您要做什么,但错误的解释很简单:gridpackplace always return None,所以像这样:

self.bar.grid(row=2, column=0).pack()

... 将与 None.pack() 相同,这正是错误告诉您的内容。

尝试在小部件上同时使用 gridpack 是没有意义的。只有一个几何管理器将控制一个小部件,因此调用 self.bar.grid() 后跟 self.bar.pack() 将丢弃 self.bar.grid() 所做的任何工作。

尝试将 self.bar.update() 添加到您的 Progress.__init__ 方法中。

progress_bar.update() 使用 progress_bar['value'] 中设置的值更新进度条,所以也许在设置进度条后调用它会显示它?

像这样:

self.popup = Toplevel()

Label(self.popup, text='Downloading Game data ...').grid(row=0, column=0, padx=20)
self.status_text = Label(self.popup, text=current_status)
self.status_text.grid(row=3, column=0, padx=20)

self.bar = Progressbar(self.popup, orient=HORIZONTAL, length=100, mode='determinate')
self.bar['value'] = self.current_percentage
self.bar.grid(row=2, column=0)
self.bar.update()