wxMessageBox 在 wxPython 中带有自动关闭计时器 [英] wxMessageBox with an auto-close timer in wxPython

查看:54
本文介绍了wxMessageBox 在 wxPython 中带有自动关闭计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

平台:Windows、OS X

Platforms: Windows, OS X

Python 版本:活动状态 Python 2.7

Python Version: Active State Python 2.7

wxPython 版本:2.9 版

wxPython Version: Version 2.9

这是我使用 wxMessageBox 的示例代码:

Here is a sample code in which I use a 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()

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

According to the above piece of code, If the host name is not 'superman' , then the user is displayed a message box and prompted to press 'OK'. If the user presses 'OK' button on the message box, then the control moves to the next line in the code (i.e., line number 10) where the frame is destroyed. I want to be to able to automatically close the dialog box and go to the next line in the code i.e., self.Destroy() if the user does not press the 'OK' button in the next 2 seconds. Any thoughts on how do I do that in wxpython ?

推荐答案

我认为您可能需要为此使用自定义 wx.Dialog.您可以使用 wx.FutureCall 调用未来的触发事件.类似的东西:

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

这篇关于wxMessageBox 在 wxPython 中带有自动关闭计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆