开发者

wxpython TextCtrl and infinite loop question

开发者 https://www.devze.com 2023-02-16 15:57 出处:网络
TextCtrl i开发者_JAVA技巧s not working when it\'s inside an infinite loop for some reason, here\'s my code:

TextCtrl i开发者_JAVA技巧s 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?


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消