We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,605 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

wxMessageBox with an auto-close timer in wxPython

Platform and Python installation info:

Platforms: Windows, OS X
Python: Active State Python 2.7
wxPython: Version 2.9

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

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 ?

3
Contributors
2
Replies
2 Days
Discussion Span
2 Years Ago
Last Updated
3
Views
Question
Answered
imperialguy
Newbie Poster
12 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

wx has a CallAfter method. It will require some implementation of multiprocessing/threads however you do it since you want to do two things at once: 1) wait for a button press, 2) keep track of the time that has passed. This programs waits for 5 seconds, simulating waiting for a button press, and after 5 seconds exits.

import time
from multiprocessing import Process

class TestClass():

   def test_f(self, name):
      ctr = 0
      while True:
         ctr += 1
         print ctr, name
         time.sleep(0.5)

if __name__ == '__main__':
   CT=TestClass()
   p = Process(target=CT.test_f, args=('Simulation of MessageBox',))
   p.start()

   ## sleep for 5 seconds and terminate
   time.sleep(5.0)
   p.terminate()
   p.join()

   print "\n\ndestroy here"
woooee
Posting Maven
2,715 posts since Dec 2006
Reputation Points: 827
Solved Threads: 780
Skill Endorsements: 9

The only wxPython widget that has a build-in timeout is wx.SplashScreen(). Your message however has be inside an image file. This can easily be done with a small wxPython program or any of the many image editors.

Take a look at:
http://www.daniweb.com/software-development/python/threads/128350/1560030#post1560030

Any of the dialog boxes rely on a button to be activated.

vegaseat
DaniWeb's Hypocrite
Moderator
6,499 posts since Oct 2004
Reputation Points: 1,451
Solved Threads: 1,618
Skill Endorsements: 37
Question Answered as of 2 Years Ago by vegaseat and woooee

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0607 seconds using 2.74MB