Search code examples
python-3.xwxpython

Wx Python calling function from events and CallLater


I have this function called OnShowCustomDialog that I want to call in 2 distinct ways, one with a mouse click on a button, and another after some time if the click didn't happen

I can do one or the other, but not both with the same code, because of a disparity in the arguments, basically if I remove "event" from the function arguments the click doesn't work, and if I keep it, the CallLater won't work

I could just make a new function to cover the other case, but I'm sure there is an easy solution I can't find

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(300,300))

        panel = wx.Panel(self, -1)
        wx.Button(panel, 1, 'Show Custom Dialog', (100,100))
        self.Bind (wx.EVT_BUTTON, self.OnShowCustomDialog, id=1)

        wx.CallLater(1000, self.OnShowCustomDialog)

    def OnShowCustomDialog(self, event):
        dia = MyDialog(self, -1, 'buttons')
        dia.Show()
        wx.CallLater(4000, dia.Destroy)

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'customdialog1.py')
        frame.Show(True)
        frame.Centre()
        return True

app = MyApp(0)
app.MainLoop()

Solution

  • you make the event optional, then it should work for both calls.

    def OnShowCustomDialog(self, event=None):
        dia = MyDialog(self, -1, 'buttons')
        dia.Show()
        wx.CallLater(4000, dia.Destroy)
    

    Michael