【发布时间】:2015-12-23 09:13:02
【问题描述】:
ProgressDialog 类允许传递选项 wx.PD_CAN_ABORT
在对话框中添加一个“取消”按钮。我需要重新绑定绑定到此的事件
按钮,使其成为Destroy() 对话框,而不仅仅是“拨打下一个电话”
to Update() [to] return False" 如类文档所述。
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
这个类以这种方式在其他地方使用:
# Scan for available ports.
port_scan_dlg = PortScanProgressDialog()
ports = port_scan_dlg.get_available_ports()
port_scan_dlg.dialog.Destroy()
当get_available_ports()发生未处理的异常时,进度
对话框将保持打开状态(这是预期的行为),但问题是
当我点击“取消”按钮是灰色的并且窗口没有关闭(点击
“X”也无法关闭窗口)。
我正在尝试将“取消”按钮重新绑定到 Destroy()s 的方法
对话。我该怎么做?
我知道这个workaround,但我认为使用ProgressDialog 更干净
并根据我的需要进行修改。
【问题讨论】:
标签: python python-2.7 wxpython