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

问题描述

我正在尝试打开一个在此处显示进度条的弹出窗口,并且遇到了一个问题,该弹出窗口直到线程完成后才打开(线程在之后开始,弹出窗口)。

此外,我可以通过在.pack()调用self.bar来使弹出窗口显示在线程开始之前,因为此错误如下:(如果出现以下错误,我将.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,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()添加到您的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,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()
,

我不知道您要做什么,但是错误的解释非常简单:gridpackplace总是返回None ,就像这样:

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

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

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