【发布时间】:2012-08-29 20:02:19
【问题描述】:
第一次发帖,但在注册之前发现该网站非常有用。 我在 Python 2.7 (Windows7) 上使用 Tkinter 时遇到问题: 代码(我已将其截断,因为整个内容很大)看起来像这样:
-------------------------------------------------------
CODE:
#set up stuff, importing variables, etc, then we have:
class App:
global RXSerial
RXSerial=''
#The following lines define the topFrame, lays out the widgets.
def __init__(self, master):
topFrame = Frame(master)
topFrame.pack()
middleFrame = Frame(master)
middleFrame.pack()
#--------------defining state variables------------
self.inputConsole = Text(middleFrame)
self.inputConsole.insert(INSERT,"Data recieved from Serial:")
self.inputConsole.config(width=100,height=20)
self.inputConsole.pack(side=LEFT,padx=20,pady=20)
#blah blah blah, insert a bunch of stuff (buttons etc.) here:
#The following lines define the functions to be called when the buttons are pressed.
def engineFire(self,engineUse,pwm):
RXSerial='this should pop up in the text called inputConsole'
print RXSerial
self.inputConsole.insert(INSERT, RXSerial)
---------------------------------------------------
是的,基本上 RXSerial 是一个字符串(我已经检查过它正在工作,当按钮调用时打印 RXSerial 行成功打印。问题是 self.inputConsole.insert(INSERT,RXSerial) 行不是工作。有人可以帮忙吗?我尝试了一堆东西的组合,但似乎无法让它工作。谢谢。
【问题讨论】:
-
你怎么称呼
engineFire?不是来自另一个线程,也许?与大多数 GUI 框架一样,Tkinter 具有线程亲和性。看这里:effbot.org/zone/tkinter-threads.htm