Hello, I'm a python beginner and glad I joined this forum :).
I'm wanting to make a simple program where the window is hidden for x seconds/minutes and is then shown again.
My problem is that using time.sleep() causes the Hide() function to not run until after time.sleep() is complete.
Here is my code. HideFrame() is the code with the problem.
import wx
import time
WINDOW_WIDTH = 200
WINDOW_HEIGHT = 100
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title = 'Sample GUI App',
pos = (200,75), size = (WINDOW_WIDTH, WINDOW_HEIGHT))
self.background = wx.Panel(self)
self.HideBtn = wx.Button(self.background, label = 'Hide the Window')
self.HideBtn.Bind(wx.EVT_BUTTON, self.HideFrame)
self.horizontalBox = wx.BoxSizer()
self.horizontalBox.Add(self.HideBtn, proportion = 0, border = 0)
self.background.SetSizer(self.horizontalBox)
self.Show()
def HideFrame(self, event):
self.Hide()
time.sleep(1)
self.Show()
print "hi"
app = wx.App(redirect=False)
window = MainFrame()
app.MainLoop()
If you run it, you will notice that the button freezes and looks pressed. The program will freeze for 1 second, then you will see the window quickly hide and show.
From what I can figure, wxPython is running Hide() as a thread and time.sleep() is freezing the whole frame, including the Hide() function. Is this correct?
If so, is there a work around?
I don't want to do a while loop to check the time, because I want the frame to be hidden for minutes and hours at a time, a loop in the background would be a lot of wasted processing.
A loop like this wouldn't be ideal.
def HideFrame(self, event):
# This still doesn't allow Hide() to complete
self.Hide()
counter = 0.1
while( counter < 1 ):
print "wasted processing power"
time.sleep(0.1)
counter += 0.1
print "done"
self.Show()
Just in case this is a platform specific problem.
I'm using Fedora 16
Python 2.7.2
wxPython 2.8.12.0