如何覆盖 ProgressDialog 的 "Cancel" 按钮事件?

How to override the "Cancel" button event of a ProgressDialog?

ProgressDialog class 允许传递选项 wx.PD_CAN_ABORT 在对话框中添加一个 "Cancel" 按钮。我需要重新绑定绑定到此的事件 按钮,使其成为 Destroy() 对话框,而不仅仅是“进行下一次调用 to Update() [to] return False”,如 class 的文档所述。

class PortScanProgressDialog(object):

    """Dialog showing progress of the port scan."""

    def __init__(self):
        self.dialog = wx.ProgressDialog(
            "COM Port Scan",
            PORT_SCAN_DLG_MSG,
            MAX_COM_PORT,
            style=wx.PD_CAN_ABORT | wx.PD_AUTO_HIDE)

    def get_available_ports(self):
        """Get list of connectable COM ports.

        :return: List of ports that e.g. exist, are not already open,
            that we have permission to open, etc.
        :rtype: list of str
        """
        com_list = []
        keep_going = True
        progress_count = 0
        for port_num in range(MIN_COM_PORT, MAX_COM_PORT + 1):
            if not keep_going:
                break
            port_str = "COM{}".format(port_num)
            try:
                # Check if the port is connectable by attempting to open
                # it.
                t_port = Win32Serial(
                    port_str, COMPATIBLE_BAUDRATE,
                    bytesize=SerialThread.BYTESIZE,
                    parity=SerialThread.PARITY,
                    stopbits=SerialThread.STOPBITS, timeout=4)
                t_port.close()
                com_list.append(port_str)
            finally:
                progress_count += 1
                # This returns a tuple with 2 values, the first of which
                # indicates if progress should continue or stop--as in
                # the case of all ports having been scanned or the
                # "Cancel" button being pressed.
                keep_going = self.dialog.Update(progress_count, msg)[0]
        return com_list

此 class 以这种方式在别处使用:

# Scan for available ports.
port_scan_dlg = PortScanProgressDialog()
ports = port_scan_dlg.get_available_ports()
port_scan_dlg.dialog.Destroy()

get_available_ports() 进度中发生未处理的异常时 对话框将保持打开状态(这是预期的行为),但问题是 当我点击 "Cancel" 时按钮变灰并且 window 没有关闭(点击 "X" 也无法关闭 window).

我正在尝试将 "Cancel" 按钮重新绑定到 Destroy() 的方法 对话。我该怎么做?

我知道这个 workaround,但我认为使用它更干净 ProgressDialog 并根据我的需要进行修改。

“取消”按钮未直接显示在此小部件中。您可以使用对话框的 GetChildren 方法访问它。这是一种方法:

import wx

class TestPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        b = wx.Button(self, -1, "Create and Show a ProgressDialog", (50,50))
        self.Bind(wx.EVT_BUTTON, self.OnButton, b)


    def OnButton(self, evt):
        max = 80

        dlg = wx.ProgressDialog("Progress dialog example",
                               "An informative message",
                               maximum = max,
                               parent=self,
                               style = wx.PD_CAN_ABORT
                                | wx.PD_APP_MODAL
                                | wx.PD_ELAPSED_TIME
                                #| wx.PD_ESTIMATED_TIME
                                | wx.PD_REMAINING_TIME
                                )
        for child in dlg.GetChildren():
            if isinstance(child, wx.Button):
                cancel_function = lambda evt, parent=dlg: self.onClose(evt, parent)
                child.Bind(wx.EVT_BUTTON, cancel_function)

        keepGoing = True
        count = 0

        while keepGoing and count < max:
            count += 1
            wx.MilliSleep(250)  # simulate some time-consuming thing...

            if count >= max / 2:
                (keepGoing, skip) = dlg.Update(count, "Half-time!")
            else:
                (keepGoing, skip) = dlg.Update(count)


        dlg.Destroy()

    #----------------------------------------------------------------------
    def onClose(self, event, dialog):
        """"""
        print "Closing dialog!"


########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title='Progress')
        panel = TestPanel(self)
        self.Show()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

这基于此特定对话框的 wxPython 演示示例。根据您的平台,您将获得本机小部件(如果存在),否则您将获得 wx.GenericProgressDialog。我怀疑本机小部件根本不允许您访问“取消”按钮,但我不确定。