468,294 Members | 1,768 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,294 developers. It's quick & easy.

wxpython and loop

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
4 2978
bartonc
6,596 Expert 4TB
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
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
zahar
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
6,596 Expert 4TB
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.

Similar topics

8 posts views Thread by Erik Johnson | last post: by
9 posts views Thread by perchef | last post: by
2 posts views Thread by rodmc | last post: by
22 posts views Thread by Glurt Wuntal | last post: by
2 posts views Thread by Kevin Walzer | last post: by
19 posts views Thread by [david] | last post: by
8 posts views Thread by Sean DiZazzo | last post: by
12 posts views Thread by bullockbefriending bard | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.