强制 ProgressDialog 在销毁时关闭

Force ProgressDialog to close upon destruction

环境: 我正在使用 wxPython Phoenix 3.0.3 和 Python 2.7.10。

情况: 我有一个按预期工作的简单 ProgressDialog,只是它在完成时不会自动关闭。对话框中有一个关闭按钮,该按钮在进度完成之前处于禁用状态。那时对话框等待用户单击关闭按钮。

问题:我想自动关闭它(不等待用户操作)。

我试过的方法:Close()Destroy() 的调用不起作用。我已经使用 Yield() 来确保所有事件都得到处理,并尝试休眠不同的时间段以确保所有堆积的事件都得到处理。

参数:我不想使用线程,这个问题太简单了,我不认为这是问题所在(但如果一个简单的解决方案是提供使用线程,就这样吧)。

最小工作示例 (MWE):

# max progress for progress dialog
max_progress = len(unparsed_files)
progress_dlg = wx.ProgressDialog("Parsing", 'Processing...', max_progress, self, 
                                 style=wx.PD_ELAPSED_TIME)

# parse files
for i, uf in enumerate(unparsed_files):
    wx.Yield()
    progress_dlg.Update(i, 'Processing {}'.format(uf))
    self.parse(uf)

progress_dlg.Update(max_progress, 'Parsing completed')
progress_dlg.Destroy()
wx.Yield()

备选解决方案: 一个没有按钮的模态对话框,不需要任何用户操作,并且可以有一个 wx.Gauge 可以更新,然后对话框被销毁。

如何在不等待用户单击“关闭”按钮(或使用没有对话框的替代对话框)的情况下关闭和销毁 ProgressDialog?

您应该将 wx.PD_AUTO_HIDE 标志添加到样式参数。

来自wxpython的documentation重点是我的):

Causes the progress dialog to disappear from screen as soon as the maximum value of the progress meter has been reached. If this style is not present, the dialog will become a modal dialog once the maximum value has been reached and wait for the user to dismiss it.