473,486 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

wxpython and loop

2 New Member
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 3455
bartonc
6,596 Recognized Expert Expert
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
gogomon
6 New Member
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 New Member
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 Recognized Expert Expert
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

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

Similar topics

16
2396
by: mike420 | last post by:
Tayss wrote: > > app = wxPySimpleApp() > frame = MainWindow(None, -1, "A window") > frame.Show(True) > app.MainLoop() > Why do you need a macro for that? Why don't you just write
8
4462
by: Erik Johnson | last post by:
I am looking for some input on GUI libraries. I want to build a Python-driven GUI, but don't really understand the playing field very well. I have generally heard good things about wxPython. I...
9
3243
by: perchef | last post by:
Hi, I have several files to download and a GUI to update. I know this is a frequently asked question but i can't find an appropriate solution. My Downloader extends threading.Thread and update a...
2
1696
by: rodmc | last post by:
I am totally new to Python and WxPython and need to write an application which can open up an external windows from a plug-in within GAIM (using pyGAIM). I have managed to hack some code together...
22
5020
by: Glurt Wuntal | last post by:
I am a newbie with Python. It's a great language, but I would like to be able to present a simple gui menu for some of my scripts; something better than using 'raw_input' prompts. Any...
6
2095
by: cyberco | last post by:
In my wxPython app a non-GUI thread (that reads info from the network) tries to open a frame to show the new info. This results in my app hanging (which is not too surprising). Coming from a C#...
2
3938
by: Kevin Walzer | last post by:
I'm porting a Tkinter application to wxPython and had a question about wxPython's event loop. The Tkinter app provides a GUI to a command-line tool. It gathers user input, and opens an...
19
3532
by: [david] | last post by:
I'd like to refresh the display before I start the main loop. I have code like this: app = App() app.Show() app.long_slow_init() app.MainLoop()
8
2635
by: Sean DiZazzo | last post by:
Is there something special you have to do to get a wxPython app to run remotely under xwindows? My Tkinter apps always automatically work that way, so I was surprised to even be confronted with...
12
2157
by: bullockbefriending bard | last post by:
I am a complete ignoramus and newbie when it comes to designing and coding networked clients (or servers for that matter). I have a copy of Goerzen (Foundations of Python Network Programming) and...
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.