Reputation: 5
I'm a very beginner of Python Programming, and now I'm wondering why my widgets cannot show number repeatedly. That is, I'd like to let the text show numbers from 1 to 9, but in the while loop, it only shows 9.... Any suggestions?
Here is my code(Python version: 2.6):
#!/user/bin/python
import wx
class Frame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self, parent, id,
'Show Number',
size = (200,150),
style=wx.MINIMIZE_BOX | wx.RESIZE_BORDER
| wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
self.initUI()
def initUI(self):
widgetPanel=wx.Panel(self, -1)
widgetPanel.SetBackgroundColour('white')
# Buttons for play the simulation
playButton = wx.Button(widgetPanel, -1, "Play", pos=(10,10), size=(30,30))
self.Bind(wx.EVT_BUTTON, self.play, playButton)
playButton.SetDefault()
# Time
self.timeText = wx.TextCtrl(widgetPanel, -1, "", pos=(10, 50),
size =(100,30), style=wx.TE_CENTER)
self.timeText.SetBackgroundColour('white')
self.timeText.SetFont(wx.Font(20, wx.DECORATIVE, wx.NORMAL, wx.NORMAL))
def play(self, event):
#self.timeText.SetValue("19:32")
self.show_num()
def show_num(self):
x = 0
while(x < 10):
self.timeText.SetValue(str(x))
x += 1
if __name__ == "__main__":
app = wx.App(False)
frame = Frame(parent=None,id=-1)
frame.Show()
app.MainLoop()
Upvotes: 0
Views: 238
Reputation: 51
I experienced an update issue with:
self.timeText.SetValue(str(x))
time.sleep(1)
x += 1
I added a wx.YieldIfNeeded() before the sleep:
self.timeText.SetValue(str(x))
wx.YieldIfNeeded()
wx.sleep(1)
x += 1
This makes sure the GUI is updated and other events are handled. I also would use the wx.Sleep(). This prevents using an other library...
Upvotes: 0
Reputation: 2295
Like the other answer said, you need to give time to your app to update. I have made a few additions to your code to make it work:
import wx
import time
import thread
class Frame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self, parent, id,
'Show Number',
size = (200,150),
style=wx.MINIMIZE_BOX | wx.RESIZE_BORDER
| wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
self.initUI()
def initUI(self):
widgetPanel=wx.Panel(self, -1)
widgetPanel.SetBackgroundColour('white')
# Buttons for play the simulation
playButton = wx.Button(widgetPanel, -1, "Play", pos=(10,10), size=(30,30))
self.Bind(wx.EVT_BUTTON, self.play, playButton)
playButton.SetDefault()
# Time
self.timeText = wx.TextCtrl(widgetPanel, -1, "", pos=(10, 50),
size =(100,30), style=wx.TE_CENTER)
self.timeText.SetBackgroundColour('white')
self.timeText.SetFont(wx.Font(20, wx.DECORATIVE, wx.NORMAL, wx.NORMAL))
def play(self, event):
#self.timeText.SetValue("19:32")
#self.show_num()
thread.start_new_thread(self.show_num,())
def show_num(self):
x = 0
while(x < 10):
self.timeText.SetValue(str(x))
time.sleep(1)
x += 1
if __name__ == "__main__":
app = wx.App(False)
frame = Frame(parent=None,id=-1)
frame.Show()
app.MainLoop()
Notice that time.sleep(1)
pauses the while loop for 1 second so that the display can be updated. You can reduce or increase the pause time as you desire.
Upvotes: 0
Reputation: 113978
you need to give your app the chance to update .... the easiest and most correct way of doing this is to use timers instead of a loop ... I have included a minimal example
import wx
app = wx.App(redirect=False)
frame = wx.Frame(None,-1,"Counter")
btn = wx.Button(f,-1,"Click Me!")
timer = wx.Timer() # this will update the count
def onUpdateButton(evt):
next_int = int(btn.GetLabel())+1
btn.SetLabel(str(next_int))
if next_int > 10:
timer.Stop()
btn.Enable(True)
def onButtonClick(event):
btn.SetLabel("0")
btn.Enable(False)
timer.Start(1000) # 1 second updates
timer.Bind(wx.EVT_TIMER,onUpdateButton)
btn.Bind(wx.EVT_BUTTON,onButtonClick)
frame.Show()
app.MainLoop()
it is probably obvious to most users, however may be worth mentioning that instead of keeping a variable with the current count i am just using the label string
Upvotes: 1