Is wxpython progressdialog cancel event possible?

谁说我不能喝 提交于 2019-12-24 09:48:27

问题


if I declare wxProgressDialog with wxPD_CAN_ABORT, "Cancel" button will be provided in ProgressDialog. Normally, to know if user pressed "Cancel", wxProgressDialog::Update needs to be called.

Is there a way to get an event, if "Cancel" is pressed in wxProgressDialog?


回答1:


You could do this by defining a custom Dialog instead of the stock ProgressDialog:

class MyProgressDialog(wx.Dialog):
    def __init__(self, parent, id, title, text=''):
        wx.Dialog.__init__(self, parent, id, title, size=(200,150), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)

        self.text = wx.StaticText(self, -1, text)
        self.gauge = wx.Gauge(self, -1)
        self.closebutton = wx.Button(self, wx.ID_CLOSE)
        self.closebutton.Bind(wx.EVT_BUTTON, self.OnClose)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.text, 0 , wx.EXPAND)
        sizer.Add(self.gauge, 0, wx.ALIGN_CENTER)
        sizer.Add(self.closebutton, 0, wx.ALIGN_CENTER)

        self.SetSizer(sizer)
        self.Show()

    def OnClose(self, event):
        self.Destroy() 
        #can add stuff here to do in parent.

You can then do updates on the progress bar by calling MyProgressDialog.gauge.Update, and have your event bound to the close button.




回答2:


Since version 2.9.1 of wx you can just use ProgressDialog.WasCancelled()

https://wxpython.org/Phoenix/docs/html/wx.GenericProgressDialog.html#wx-genericprogressdialog



来源:https://stackoverflow.com/questions/5910193/is-wxpython-progressdialog-cancel-event-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!