WxPython:在 TextCtrl 中定期设置值不起作用 [英] WxPython: Periodically set value in TextCtrl not working

查看:32
本文介绍了WxPython:在 TextCtrl 中定期设置值不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 和 OOP 的新手,通常做 C &VHDL.我必须定期在窗口"中显示一个值,但似乎无法实现.照原样,该值仅提取一次并且 Quit 不起作用.我试图根据我读过的许多帖子修改它,但我无法让它工作.任何帮助将不胜感激.

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.

这是我在论坛上的第二篇文章.如果我的帖子不正确,请告知.使用 Python 2.7、wxPython 2.8、Windows 7

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()

推荐答案

如果使用线程,则需要使用线程安全的方法来更新 UI,例如 wx.CallAfter 或 wx.PostEvent.但是,由于您只是根据时间更新值而不是使用长时间运行的进程,因此我认为您可以使用 wx.Timer 而不是线程.以下是有关该主题的几个链接:

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:

这篇关于WxPython:在 TextCtrl 中定期设置值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆