python - wxPython:ProgressDialog 崩溃

标签 python wxpython

我有这个建立连接的程序。当它尝试连接时,它会创建一个 ProgressDialog。当连接失败时,它应该干净地关闭 ProgressDialog,但它没有,它会出现段错误。

这是相关代码的简化示例。您可以通过运行此脚本并多次按回车键来复制此行为,这将单击应用中的唯一按钮并触发代码以尝试连接。

到目前为止,我只能弄清楚该应用程序只会在第 53 行:'print('after_pulse')' 未运行时崩溃,这表明“Pulse”调用有问题,但我可以'弄清楚是什么。

有谁知道是什么导致这段代码崩溃?

如果没有,您至少可以复制这种行为吗?

规范:

Ubuntu 12.04

python 2.7.3

wxPython 2.8.12.1

编辑:没有扭曲代码的更小的示例程序。

#!/usr/bin/env python

'''test2'''

import wx   # Must be imported before any other wx modules


class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='TestFrame')
        self.button = wx.Button(self, label='test')
        self.button.Bind(wx.EVT_BUTTON, self.button_handler)
        self.button.SetFocus()

    def button_handler(self, event):
        pd_style = wx.PD_APP_MODAL|wx.PD_CAN_ABORT|wx.PD_SMOOTH
        self.pd = wx.ProgressDialog('test_title', 'test_msg', parent=self,  
                                    style=pd_style)
        self.pd.SetFocus()

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
        self.timer.Start(1000/60, False)

        wx.CallAfter(self.err_connection)

    def on_timer(self, event):
        print('before_pulse')
        cont, _skip = self.pd.Pulse()
        print('after_pulse')
        if not cont:
            self.err_connection()

    def err_connection(self):
        print('err_connection')
        self.timer.Stop()
        self.pd.Destroy()
        self.button.SetFocus()


def main():
    app = wx.App()
    frame = TestFrame()
    frame.Show()
    app.MainLoop()


if __name__ == '__main__':
    main()

最佳答案

我怀疑您正在破坏仍在执行最后一个 Pulse() 过程中的进度对话框。有时,某些平台会将特定 API 的部分处理推迟到稍后,在第一部分完成之后或其他任何事情之后。如果您延迟对 Destroy 的调用,您可能会得到更好的结果。尝试:

wx.CallAfter(self.pd.Destroy)

此外,每秒 60 次对于更新 UI 元素之类的事情来说已经非常快了。这比肉眼所能看到的要快得多,并且可能导致一些事件堆积起来并以意外的顺序调用处理程序,例如获取计时器事件并在对话框被销毁后尝试执行 Pulse() 。改为每秒尝试 10 或 20 次。

关于python - wxPython:ProgressDialog 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11591067/

相关文章:

python - Django Rest Framework RelatedField 无法返回字典对象

python - 使用 OR 时 re 不匹配

python ObjectListView 用空行更新显示,即使列表具有有效数据

python - wxPython:从文件绘制基于矢量的图像

python - wxPython:在现有 wx.Panel 上覆盖 wx.Panel 的好方法

python - 我们如何在 PyMC3 的分层模型中预测新的看不见的组?

python - 解构时更改数据类型

python - 如何解决 Django 管理中的 "Could not import django.contrib.syndication.views.feed"错误?

python - wxPython:禁用笔记本选项卡?

python - Destroy() wxpython 简单错误?