#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Posts
    8
    Rep Power
    0

    for and time block the wx render


    Hello everyone! I created this code but it does not work.
    operation:
    creates an interface with button and loading bar
    clicking on the button should fill the bar
    unfortunately does not go, nothing happens during the cycle, the cycle ends when the loading bar fills me in one fell swoop.
    why?
    thanks
    Code:
    import wx
    import time
    
    class GUI(wx.Frame):
    	def __init__(self, *args, **kwargs):
    		super(GUI, self).__init__(*args, **kwargs)             
    		
    		pnl = wx.Panel(self)
    		vbox = wx.BoxSizer(wx.VERTICAL)
    
    		self.perc = wx.Gauge(pnl, range=100, size=(250, 30))
    		vbox.Add(self.perc, flag=wx.ALL, border=5)
    
    		but = wx.Button(pnl, wx.ID_CLOSE, "Button")
    		vbox.Add(but, flag=wx.ALL, border=5)
    		but.Bind(wx.EVT_BUTTON, lambda event: self.update())
    
    		pnl.SetSizer(vbox)
    		self.Show(True)
    	def update(self):
    		for count in range(1,100):
    			self.perc.SetValue(count)
    			time.sleep(0.1)
    
    ex = wx.App()
    Gui=GUI(None, style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
    ex.MainLoop()
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,908
    Rep Power
    509
    (untested)

    Older pythons won't reference self in the anonymous function within
    but.Bind(wx.EVT_BUTTON, lambda event: self.update())

    I'd first change that statement to
    but.Bind(wx.EVT_BUTTON, self.update)
    and correspondingly
    def update(self, *args, **kwargs):

    when those changes don't help call someone else.

    Comments on this post

    • Will-O-The-Wisp agrees
    Last edited by b49P23TIvg; April 23rd, 2015 at 07:52 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Posts
    8
    Rep Power
    0
    I noticed something ...
    on windows, my script works perfectly.
    while on linux ubuntu does not work, even with the changes you suggested.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    656
    Rep Power
    39
    First, it is not a good idea to use time.sleep() in any GUI as it can block the GUI's infinite loop. In Wx use timer or CallAfter instead. I do not know what "does not work" means, so am assuming that the gauge does not get updated until after the for() loop finishes which is a common problem when you block the GUI's infinite loop. So you might try a Refresh() in the for() loop to force the interrupted GUI to update, and/or timer (and timer.Stop() after 100 iterations), or CallAfter.

    Comments on this post

    • Will-O-The-Wisp agrees
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Posts
    8
    Rep Power
    0
    perfect! thanks a lot!
    inserting self.Update() in the "for" works! and also using timer

IMN logo majestic logo threadwatch logo seochat tools logo