如何解决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()
,
我不知道您要做什么,但是错误的解释非常简单:grid
,pack
,place
总是返回None
,就像这样:
self.bar.grid(row=2,column=0).pack()
...与None.pack()
相同,这正是错误告诉您的内容。
尝试在小部件上同时使用grid
和pack
是没有意义的。只有一个几何管理器将控制一个小部件,因此调用self.bar.grid()
后跟self.bar.pack()
将会丢弃self.bar.grid()
完成的所有工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。