wxPython TextCtrl 框背景不更新?

wxPython TxtCtrl box background not updating?

我正在 运行遇到一个简单程序的问题,该程序在通过 TCP 从另一台机器接收到命令时更改背景颜色。只有当我 运行 我的鼠标悬停在 window 上时,背景才会更新。我正在 运行 通过本地网络进行此操作,因此它应该接近即时响应。我正在为框架使用 wxPython。我不认为我有过于复杂的代码。相关代码:

    threader=threading.Thread(target=self.threading)
    threader.start()
def threading(self):
    host="192.168.1.122"
    port=4100
    s=socket.socket()
    s.bind((host,port))
    s.listen(1)
    c,addr=s.accept()
    print "Connected"
    while 1:
        data=c.recv(1024)
        if not data:
            break
        data=data.split("_")
        reading=int(data[1])
        pin=int(data[0])
        if pin == 1:
            if reading<20:
                self.front_left.SetBackgroundColour("red")
        elif pin == 2:
            if reading<20:
                self.front_right.SetBackgroundColour("red")
        elif pin == 3:
            if reading<20:
                self.bottom_left.SetBackgroundColour("red")
        elif pin == 4:
            if reading<20:
                self.bottom_right.SetBackgroundColour("red")
        else:
            pass
    c.close()

我需要这个代码是即时的,因为这将在一个机器人上进行,它会判断物体是否太近(这就是为什么当它到达物体 20 厘米以内时会出现红色背景)。任何帮助将不胜感激!

你最好不要在其他线程调用wx函数。将所有 UI 保留在主线程中并以某种方式与其通信。我将事件用于类似目的,所以我可能会定义自己的事件,

(MyOwnEvent, EVT_MY_OWN) = wx.lib.newevent.NewEvent()

在主线程中处理,

self.Bind(lib.BarCode.EVT_MY_OWN, self.OnMyOwn)

并从处理 TCP 的线程调用

wx.PostEvent(main_window, MyOwnEvent())

或者如果您想简化您的工作,只需使用wx.CallAfter设置颜色,然后可能会刷新。