By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,036 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,036 IT Pros & Developers. It's quick & easy.

wxpython and loop

P: 2
Hi, I'm a newbie struggling to understand wxpython, much appreciated if you guys can solved my problem. Try googling but still does not have any clue on how to stop the loop by pressing the stop button.

Window XP / Python 2.5 /wxpython 2.8
Expand|Select|Wrap|Line Numbers
  1. import wx
  2.  
  3. global OK
  4.  
  5. def Start(event):
  6.  
  7.         OK=1
  8.  
  9.         while OK:
  10.                 # Hopefully  this GUI take input from external device
  11.                 print "Testing"
  12.  
  13.         # close port
  14.  
  15. def Stop(event):
  16.         OK=0
  17.  
  18.  
  19. app=wx.App()
  20. win=wx.Frame(None,title="Simple Editor",size=(410,335))
  21.  
  22. bkg=wx.Panel(win)
  23.  
  24. loadButton=wx.Button(bkg,label='Start')
  25. loadButton.Bind(wx.EVT_BUTTON,Start)
  26.  
  27. saveButton=wx.Button(bkg,label='Stop')
  28. saveButton.Bind(wx.EVT_BUTTON,Stop)
  29.  
  30.  
  31. contents=wx.TextCtrl(bkg,style=wx.TE_MULTILINE)
  32.  
  33.  
  34. hbox = wx.BoxSizer()
  35.  
  36. hbox.Add(loadButton,proportion=0,border=2)
  37. hbox.Add(saveButton,proportion=0,border=2)
  38.  
  39.  
  40. vbox = wx.BoxSizer(wx.VERTICAL)
  41. vbox.Add(contents,proportion=1,flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=5)
  42. vbox.Add(hbox,proportion=0,flag=wx.EXPAND|wx.ALL,border=5)
  43.  
  44. bkg.SetSizer(vbox)
  45.  
  46. win.Show()
  47.  
  48. app.MainLoop()
  49.  
May 23 '07 #1
Share this Question
Share on Google+
4 Replies


bartonc
Expert 5K+
P: 6,596
Hi, I'm a newbie struggling to understand wxpython, much appreciated if you guys can solved my problem. Try googling but still does not have any clue on how to stop the loop by pressing the stop button.

Window XP / Python 2.5 /wxpython 2.8
Expand|Select|Wrap|Line Numbers
  1. import wx
  2.  
  3. global OK
  4.  
  5. def Start(event):
  6.  
  7.         OK=1
  8.  
  9.         while OK:
  10.                 # Hopefully  this GUI take input from external device
  11.                 print "Testing"
  12.  
  13.         # close port
  14.  
  15. def Stop(event):
  16.         OK=0
  17.  
  18.  
  19. app=wx.App()
  20. win=wx.Frame(None,title="Simple Editor",size=(410,335))
  21.  
  22. bkg=wx.Panel(win)
  23.  
  24. loadButton=wx.Button(bkg,label='Start')
  25. loadButton.Bind(wx.EVT_BUTTON,Start)
  26.  
  27. saveButton=wx.Button(bkg,label='Stop')
  28. saveButton.Bind(wx.EVT_BUTTON,Stop)
  29.  
  30.  
  31. contents=wx.TextCtrl(bkg,style=wx.TE_MULTILINE)
  32.  
  33.  
  34. hbox = wx.BoxSizer()
  35.  
  36. hbox.Add(loadButton,proportion=0,border=2)
  37. hbox.Add(saveButton,proportion=0,border=2)
  38.  
  39.  
  40. vbox = wx.BoxSizer(wx.VERTICAL)
  41. vbox.Add(contents,proportion=1,flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=5)
  42. vbox.Add(hbox,proportion=0,flag=wx.EXPAND|wx.ALL,border=5)
  43.  
  44. bkg.SetSizer(vbox)
  45.  
  46. win.Show()
  47.  
  48. app.MainLoop()
  49.  
While this in not good structure, it is very common for beginners. Here's what it would look like to do what you want:
Expand|Select|Wrap|Line Numbers
  1.  
  2. OK = 0
  3.  
  4. def Start(event):
  5.     global OK
  6.  
  7.         OK=1
  8. # .....
  9.  
  10. def Stop(event):
  11.     global OK
  12.     OK=0
May 23 '07 #2

P: 6
Well from what I am seeing it seems that the application would get stuck in an infinite loop inside of the while OK loop. and therefore even if you press the stop button it can't process it since its stuck inside of the start function inside that while OK loop.

You might want to look into threads and how to spawn them to do your bidding without blocking the main one.
May 23 '07 #3

P: 2
Well from what I am seeing it seems that the application would get stuck in an infinite loop inside of the while OK loop. and therefore even if you press the stop button it can't process it since its stuck inside of the start function inside that while OK loop.

You might want to look into threads and how to spawn them to do your bidding without blocking the main one.
Find difficulty in using threads, has to settle for wx.Yield() inside the loop. It works, but not as elegant as thread
May 28 '07 #4

bartonc
Expert 5K+
P: 6,596
Find difficulty in using threads, has to settle for wx.Yield() inside the loop. It works, but not as elegant as thread
Thanks for the update. Glad that you got it working.
May 28 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.