lamba
lamba

Reputation: 1651

wxpython TextCtrl and infinite loop question

TextCtrl is not working when it's inside an infinite loop for some reason, here's my code:

   while 1:
        localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        i = i + 1
        #print str(i)

        serRead = ser.readline()
        serSplit = serRead.split(",")

        #this works
        print str(i)+', '+tempOrShock+', '+localtime+', '+serSplit[1]

        #this doesn't
        self.displayTextCtrl.WriteText(str(i)+', '+tempOrShock+', '+
                                        localtime+', '+serSplit[1])

This infinite while loop is inside a button click event, I basically run an infinite loop after a button is clicked and tell my TextCtrl to continuously write stuff out and it is not working. However, the print statement works fine. Any idea why this might be the case?

Upvotes: 1

Views: 1018

Answers (3)

Bryan Oakley
Bryan Oakley

Reputation: 385940

GUI programs work by having an infinite loop that pulls events off of a queue and executes them. In addition to user generated events such as button presses, there are low level events that tell widgets to draw themselves. Unless these events get processed the window wont redraw even if you modify their properties (such as changing colors, adding text, etc). Because you have your own infinite loop you are preventing these low level events from being processed.

A naive solution is, from within your own loop you pull events off the queue and process them. Since there is already an infinite loop running (the "event loop") a better solution is to not write your own infinite loop. Instead, use wx.CallAfter or wx.CallLater to add work to be performed once the low level events have been processed (ie: when the GUI is "idle")

In effect, each call to wx.CallAfter becomes one iteration of your own loop.

Upvotes: 3

Miki Tebeka
Miki Tebeka

Reputation: 13850

You should probably use wx.Timer instead of infinite loop. Another option to explore is wx.Yield.

Upvotes: 3

Fred Larson
Fred Larson

Reputation: 62063

I suspect wxpython has some processing it needs to do (dispatching events and so forth) in its main loop. But until you return from your event handler, that loop can't run. You'd probably be better off setting up a timer to update your text control periodically.

Upvotes: 6

Related Questions