wxpython and loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zahar
    New Member
    • May 2007
    • 2

    #1

    wxpython and loop

    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
    [CODE=python]import wx

    global OK

    def Start(event):

    OK=1

    while OK:
    # Hopefully this GUI take input from external device
    print "Testing"

    # close port

    def Stop(event):
    OK=0


    app=wx.App()
    win=wx.Frame(No ne,title="Simpl e Editor",size=(4 10,335))

    bkg=wx.Panel(wi n)

    loadButton=wx.B utton(bkg,label ='Start')
    loadButton.Bind (wx.EVT_BUTTON, Start)

    saveButton=wx.B utton(bkg,label ='Stop')
    saveButton.Bind (wx.EVT_BUTTON, Stop)


    contents=wx.Tex tCtrl(bkg,style =wx.TE_MULTILIN E)


    hbox = wx.BoxSizer()

    hbox.Add(loadBu tton,proportion =0,border=2)
    hbox.Add(saveBu tton,proportion =0,border=2)


    vbox = wx.BoxSizer(wx. VERTICAL)
    vbox.Add(conten ts,proportion=1 ,flag=wx.EXPAND |wx.LEFT|wx.RIG HT,border=5)
    vbox.Add(hbox,p roportion=0,fla g=wx.EXPAND|wx. ALL,border=5)

    bkg.SetSizer(vb ox)

    win.Show()

    app.MainLoop()
    [/CODE]
    Last edited by bartonc; May 23 '07, 06:00 PM. Reason: fixed [code][/code] tags
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by zahar
    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
    [CODE=python]import wx

    global OK

    def Start(event):

    OK=1

    while OK:
    # Hopefully this GUI take input from external device
    print "Testing"

    # close port

    def Stop(event):
    OK=0


    app=wx.App()
    win=wx.Frame(No ne,title="Simpl e Editor",size=(4 10,335))

    bkg=wx.Panel(wi n)

    loadButton=wx.B utton(bkg,label ='Start')
    loadButton.Bind (wx.EVT_BUTTON, Start)

    saveButton=wx.B utton(bkg,label ='Stop')
    saveButton.Bind (wx.EVT_BUTTON, Stop)


    contents=wx.Tex tCtrl(bkg,style =wx.TE_MULTILIN E)


    hbox = wx.BoxSizer()

    hbox.Add(loadBu tton,proportion =0,border=2)
    hbox.Add(saveBu tton,proportion =0,border=2)


    vbox = wx.BoxSizer(wx. VERTICAL)
    vbox.Add(conten ts,proportion=1 ,flag=wx.EXPAND |wx.LEFT|wx.RIG HT,border=5)
    vbox.Add(hbox,p roportion=0,fla g=wx.EXPAND|wx. ALL,border=5)

    bkg.SetSizer(vb ox)

    win.Show()

    app.MainLoop()
    [/CODE]
    While this in not good structure, it is very common for beginners. Here's what it would look like to do what you want:
    Code:
    OK = 0
    
    def Start(event):
        global OK
            
            OK=1
    # .....
    
    def Stop(event):
        global OK
        OK=0

    Comment

    • gogomon
      New Member
      • May 2007
      • 6

      #3
      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.

      Comment

      • zahar
        New Member
        • May 2007
        • 2

        #4
        Originally posted by gogomon
        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

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by zahar
          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.

          Comment

          Working...