use*_*540 4 python wxpython auto-close
平台:Windows,OS X.
Python版本:Active State 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()
Run Code Online (Sandbox Code Playgroud)
根据上面的代码,如果主机名不是"超人",则会在用户显示一个消息框并提示按"确定".如果用户按下消息框上的"确定"按钮,则控制移动到代码中的下一行(即行号10),其中帧被销毁.我希望能够自动关闭对话框并转到代码中的下一行,即self.Destroy()如果用户在接下来的2秒内未按下"确定"按钮.有关如何在wxpython中执行此操作的任何想法?
如果通过继承wx.Dialog创建自己的自定义对话框,则可以使用wx.Timer生成一个周期性事件,您可以绑定一个处理程序,每次定时器事件触发时都会更新消息,然后在x事件触发后可以销毁对话框.
工作范例:
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4737 次 |
| 最近记录: |