Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using time.sleep in wxPython

Using time.sleep in my wxPython code just after re-positioning a bitmapbutton caused my button to go totally blank. Just a white space was left in the region where the button should have been. Can any one please explain the reason and suggest any solution? Here's my code:

import wx
import time
class gui(wx.Frame):
  def __init__(self,parent,id):
    wx.Frame.__init__(self,parent,id,'New Window',pos=(0,0),size=wx.DisplaySize())
    panel=wx.Panel(self)
    self.SetBackGroundColour('green')
    self.pic=wx.BitmapButton(self,-1,wx.Image("Candle.jpg",wx.BITMAP_TYPE_ANY).ConvertToBitmap(),pos=(700,300))
    self.Bind(wx.EVT_BUTTON,self.position,self.pic)
  def positon(self,event):
    self.pic.Hide()
    self.pic=wx.BitmapButton(self,-1,wx.Image("Candle.jpg",wx.BITMAP_TYPE_ANY).ConvertToBitmap(),pos=(700,300))
    time.sleep(2)
    self.pic.Hide()
if __name__=='__main__':
  app=wx.PySimpleApp()
  frame=gui(None,-1)
  frame.Show()
  app.MainLoop()
like image 631
zer0nes Avatar asked Mar 09 '26 12:03

zer0nes


2 Answers

time.sleep() blocks wx's mainloop and makes the GUI unresponsive for however long you've told it to sleep. There are several alternatives. You can use a wx.Timer or use threads (or similar). I think using a Timer makes more sense in your use case though.

like image 66
Mike Driscoll Avatar answered Mar 11 '26 00:03

Mike Driscoll


Well there's no wonder your button goes blank, you've pretty much programmed it to do so.

    self.pic.Hide() => hides the button
 self.pic=wx.BitmapButton(self,-1,wx.Image("Candle.jpg",wx.BITMAP_TYPE_ANY).ConvertToBitmap(),pos=(700,300)) => displays the button once again
    time.sleep(2) => takes a brake for 2 seconds
    self.pic.Hide() => hides the button again

The conclusion is, your button won't show up. So I don't see what's the problem, as it does exactly what you programmed it to.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!