WxPython: Periodically set value in TextCtrl not w

2019-09-16 23:51发布

I am new to Python and OOP, usually do C & VHDL. I have to periodically display a value in a “window”, but can’t seem to make it. As is, the value is only fetched once and Quit doesn't work. I tried to modify it based on many posts that I have read, but I can’t get it to work. Any help would be much appreciated.

This is my second post ever on a forum. Please inform if my post is not as it should be. Using Python 2.7, wxPython 2.8, Windows 7

import wx
import threading

class RspBox(wx.Frame):


    def __init__(self, parent, title):
        super(RspBox, self).__init__(parent, title=title, size=(500, 500))  
        self.InitUI()
        self.Centre()
        self.Show()    

    def InitUI(self):
        sizer = wx.GridBagSizer(7, 7)#(y,x)

        self.Ybtn = wx.Button(self, label='Yellow')
        self.Gbtn = wx.Button(self, label='Green')
        self.Wbtn = wx.Button(self, label='White')
        self.Rbtn = wx.Button(self, label='Red')
        self.Bbtn = wx.Button(self, label='Blue')
        self.ApplyBtn = wx.Button(self, id=wx.ID_APPLY, label='')
        self.QuitBtn = wx.Button(self, id=wx.ID_ANY, label='Quit')
        self.Label1 = wx.StaticText(self, label='Direction')
        self.Label2 = wx.StaticText(self, label='Value')
        self.Dir = wx.TextCtrl(self, style=wx.TE_LEFT, value='000000')
        self.Val = wx.TextCtrl(self, style=wx.TE_READONLY, value='CAFE')
        self.t = self.SetDirection()

        sizer.Add(self.Ybtn, pos=(1, 2), span=(1, 1))
        sizer.Add(self.Gbtn, pos=(2, 1), span=(1, 1))
        sizer.Add(self.Wbtn, pos=(2, 2), span=(1, 1))
        sizer.Add(self.Rbtn, pos=(2, 3), span=(1, 1))
        sizer.Add(self.Bbtn, pos=(3, 2), span=(1, 1))
        sizer.Add(self.Label1, pos=(4, 1), span=(1, 1))
        sizer.Add(self.Dir, pos=(4, 2), span=(1, 2))
        sizer.Add(self.Label2, pos=(5, 1), span=(1, 1))
        sizer.Add(self.Val, pos=(5, 2), span=(1, 2))
        sizer.Add(self.ApplyBtn, pos=(6, 2), span=(1, 1))
        sizer.Add(self.QuitBtn, pos=(6, 3), span=(1, 1))

        self.Bind(wx.EVT_BUTTON, self.stop(self), self.QuitBtn)

        self.SetSizer(sizer)
        self.Fit()
        self.Show()


    def SetDirection(self):
        """Set the TextCtrl Direction field periodically"""
        Val = 3 #debug
        self.Val.SetValue(str(Val))
        t = threading.Timer(1, self.SetDirection)
        t.start()
        print "SetDirection = %s" % Val #debug
        return t


    def stop(self, event):
        self.t.cancel()


if __name__ == '__main__':
    print test
    app = wx.App()
    RspBox(None, title='ResponseBox')
    app.MainLoop()

1条回答
狗以群分
2楼-- · 2019-09-17 00:41

If you use threads, then you need to use a thread-safe method to update the UI, such as wx.CallAfter or wx.PostEvent. However, since you're just updating the value based on time instead of using a long-running process, I think you could use a wx.Timer instead of a thread. Here are a couple of links on the topic:

查看更多
登录 后发表回答