wxMessageBox with an auto-close timer in wxPython

你离开我真会死。 提交于 2019-12-04 10:46:25

I think you might have to use a custom wx.Dialog for this. You can use wx.FutureCall to call trigger an event in the future. Something like:

class MessageDialog(wx.Dialog):
    def __init__(self, message, title):
        wx.Dialog.__init__(self, None, -1, title,size=(300, 120))
        self.CenterOnScreen(wx.BOTH)

        ok = wx.Button(self, wx.ID_OK, "OK")
        ok.SetDefault()
        text = wx.StaticText(self, -1, message)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(text, 1, wx.ALIGN_CENTER|wx.TOP, 10)
        vbox.Add(ok, 1, wx.ALIGN_CENTER|wx.BOTTOM, 10)
        self.SetSizer(vbox)

class Frame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(100, 100),style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN)

        host=os.system('hostname')
        if host!='superman':
            dlg = MessageDialog('The host name should be superman. Closing this dialog box in 2s...', 'Info')        
            wx.FutureCall(2000, dlg.Destroy)
            dlg.ShowModal()
        else:
            self.Center()
            self.Show()

If you create your own custom dialog by subclassing wx.Dialog you can use a wx.Timer to generate a periodic event to which you can bind a handler which updates the message every time the timer event fires, then after x event fires you can destroy the dialog.

Working example:

import wx
import os

class MessageDialog(wx.Dialog):
    def __init__(self, message, title, ttl=10):
        wx.Dialog.__init__(self, None, -1, title,size=(400, 150))
        self.CenterOnScreen(wx.BOTH)
        self.timeToLive = ttl

        stdBtnSizer = self.CreateStdDialogButtonSizer(wx.OK|wx.CANCEL) 
        stMsg = wx.StaticText(self, -1, message)
        self.stTTLmsg = wx.StaticText(self, -1, 'Closing this dialog box in %ds...'%self.timeToLive)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(stMsg, 1, wx.ALIGN_CENTER|wx.TOP, 10)
        vbox.Add(self.stTTLmsg,1, wx.ALIGN_CENTER|wx.TOP, 10)
        vbox.Add(stdBtnSizer,1, wx.ALIGN_CENTER|wx.TOP, 10)
        self.SetSizer(vbox)

        self.timer = wx.Timer(self)
        self.timer.Start(1000)#Generate a timer event every second
        self.timeToLive = 10 
        self.Bind(wx.EVT_TIMER, self.onTimer, self.timer)

    def onTimer(self, evt):
        self.timeToLive -= 1
        self.stTTLmsg.SetLabel('Closing this dialog box in %ds...'%self.timeToLive)

        if self.timeToLive == 0:
            self.timer.Stop()
            self.Destroy()

class Frame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(100, 100),style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN)

        host=os.system('hostname')
        if host!='superman':
            dlg = MessageDialog('The host name should be superman', 'Info', ttl=10)               
            dlg.ShowModal()
        else:
            self.Center()
            self.Show()

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = Frame(None, -1, "") 
    frame.Show(1)
    app.MainLoop()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!