Search code examples
pythonwxpython

Is wxpython progressdialog cancel event possible?


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?


Solution

  • 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.