1

This is the code that i have written. It does close the window but doesnt display the text in it. I need the text displayed and then automatic close of the window. What changes should i make for it to work Thanks

Here is the code

import wx
from time import sleep

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, size=(300,200))

        self.panel = wx.Panel(self)
        box = wx.BoxSizer(wx.VERTICAL)
        m_text = wx.StaticText(self.panel, -1, 'File Uploaded!')
        m_text.SetSize(m_text.GetBestSize())

        box.Add(m_text, 0, wx.ALL, 10)
        self.panel.SetSizer(box)
        self.panel.Layout()
        self.Bind(wx.EVT_ACTIVATE, self.onClose)

    def onClose(self, event):
        sleep(5)
        self.Destroy()

app = wx.App(redirect=True)
top = Frame('test')
top.Show()
app.MainLoop()
kush87
  • 529
  • 1
  • 6
  • 11
  • duplicate of http://stackoverflow.com/questions/6012380/wxmessagebox-with-an-auto-close-timer-in-wxpython, take a look at that one. – Corley Brigman Oct 08 '13 at 12:58

2 Answers2

2

I would recommend using a wx.Timer. If you use time.sleep(), you will block wxPython's main loop which makes your application unresponsive. Here is your code modified to use the timer:

import wx

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, size=(300,200))

        self.panel = wx.Panel(self)
        box = wx.BoxSizer(wx.VERTICAL)
        m_text = wx.StaticText(self.panel, -1, 'File Uploaded!')
        m_text.SetSize(m_text.GetBestSize())

        box.Add(m_text, 0, wx.ALL, 10)
        self.panel.SetSizer(box)
        self.panel.Layout()

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onClose, self.timer)
        self.timer.Start(5000)

    def onClose(self, event):
        self.Close()

app = wx.App(redirect=True)
top = Frame('test')
top.Show()
app.MainLoop()

You can read more about timers in this article:

http://www.blog.pythonlibrary.org/2009/08/25/wxpython-using-wx-timers/

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
1
>>> import wx
>>> import time
>>> app = wx.App()
>>> b = wx.BusyInfo('Upload Finished!')
>>> time.sleep(5)
>>> del b
>>>
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73