Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wxMessageBox在wxPython中具有自动关闭计时器_Python_Wxpython_Auto Close - Fatal编程技术网

wxMessageBox在wxPython中具有自动关闭计时器

wxMessageBox在wxPython中具有自动关闭计时器,python,wxpython,auto-close,Python,Wxpython,Auto Close,平台:Windows、OS X Python版本:活动状态Python 2.7 wxPython版本:2.9版 下面是我使用wxMessageBox的示例代码: import wx,os class Frame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(100, 100),style=wx.MINIMIZE_BOX

平台:Windows、OS X

Python版本:活动状态Python 2.7

wxPython版本:2.9版

下面是我使用wxMessageBox的示例代码:

import wx,os

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':            
            self.dialogBox=wx.MessageBox('The host name should be superman. Closing this dialog box in 2s...','Info')            
            self.Destroy()
        else:
            self.Center()
            self.Show()

if __name__ == '__main__':
    app = wx.App(redirect=False)
    frame = Frame(None, -1, 'Sample')
    app.MainLoop()

根据上述代码,如果主机名不是“超人”,则会显示一个消息框,提示用户按“确定”。如果用户按下消息框上的“OK”(确定)按钮,则控件将移动到代码中的下一行(即第10行),其中帧将被销毁。我希望能够自动关闭对话框并转到代码中的下一行,即,
self.Destroy()
,如果用户在接下来的2秒钟内没有按下“OK”按钮。关于如何在wxpython中做到这一点,您有什么想法吗?

我想您可能需要为此使用一个自定义的
wx.Dialog
。您可以使用
wx.FutureCall
在将来调用触发器事件。比如:

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()

如果通过子类化创建自己的自定义对话框,则可以使用生成一个周期性事件,可以将处理程序绑定到该事件,该处理程序在每次触发计时器事件时更新消息,然后在触发x事件后,可以销毁该对话框

工作示例:


很好的解决方案。谢谢我们可以让消息随着时间的推移显示计时器,意思是“3秒内关闭…”、“2秒内关闭…”、“1秒内关闭…”然后关闭。当然可以,尽管我对wxPython还不太熟悉,无法确切说明如何关闭。可能需要检查以下代码:使用wx.Timer,您可以绑定到它的EVT_计时器,例如-->,这样您就可以在每次触发计时器事件时更新用户离开的时间,然后在x事件触发销毁后…稍后有时间,我将添加一个完整答案,并给出一个示例。这是一个很好的解决方案,但在Python2.7.12上使用wx 4.0.1时,我得到了
WxPydePreactionWarning:使用不推荐使用的类。改用CallLater。
。只需将上述答案中的
wx.FutureCall
更改为
wx.CallLater
即可删除警告。
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()