Search code examples
pythonpython-3.xtimewxpythonmessage

Show a limited time Message wxPython


I build an application in wxPython and in the application I want to show a message for a limited time and then, it will be disappeared by itself (the user of the application doesn't have to nothing to make it disappear) I tried to do it like that, but it didn't closed by itself.

dialog = wx.MessageDialog(None,'message', wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP)
threading.Timer(2.0, dialog.Destroy).start()
dialog.ShowModal()

I also tried to do it like that and it either didn't do nothing:

dialog = wx.MessageDialog(None,'message', wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP)
threading.Timer(2.0, dialog.EndModal,args=wx.ID_OK).start()
dialog.ShowModal()

Solution

  • I don't have enough context to see what is the problem with your timer. You could try this however.

    import wx
    
    # ================================================================================
    class TimedDialog(wx.Dialog):
    
        def __init__(self, *args, **kwargs):
            super(TimedDialog, self).__init__(*args, **kwargs)
    
            self.SetSize((400, 300))
            self.SetTitle('Please wait!')
            self.Centre()
    
            self.timer = wx.Timer(self)
            self.Bind(wx.EVT_TIMER, self.OnTimer)
    
            self.timer.Start(2000)  # 2 second interval
    
        def OnTimer(self, event):
            self.Close()
    
    
    # ================================================================================
    class Example(wx.Frame):
    
        def __init__(self, *args, **kwargs):
            super(Example, self).__init__(*args, **kwargs)
    
            self.InitUI()
    
        def InitUI(self):
    
            self.SetSize((300, 200))
            self.SetTitle('app')
            self.Centre()
            self.btn = wx.Button(self, -1, "click Me")
            self.btn.Bind(wx.EVT_BUTTON, self.OnClicked)
    
        def OnClicked(self, event):
            dlg = TimedDialog(self)
            dlg.ShowModal()
    
    
    def main():
    
        app = wx.App()
        ex = Example(None)
        ex.Show()
        app.MainLoop()
    
    
    if __name__ == '__main__':
        main()