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

The code perfect for me. windows 7, python 2.7.2, wxpython 2.9.3.1
You can try updating wx or use the wx.CallAfter method which calls whatever you pass to it after the current event has finished processing. Maybe something like

def HideFrame(self, evt):
     self.Hide()
     wx.CallAfter(self.Wait)

def Wait(self):
   time.sleep(1)
   self.Show()

Thank you, ihatehippies

I see that the function CallAfter will run Wait() after HideFrame() has finished.

It works perfectly for my program. I haven't tried to find out if it was a compatibility problem. Using CallAfter makes it more compatible with more platforms.

I wasn't expecting such an easy answer, you saved me hours of googling.
Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.