Tkinter GUI程序的多线程问题,,继承Tkinter.Event 求高手指点

XIANXir 2011-08-22 09:06:02
在写一个串口终端,界面如下:


我是这样实现的,建立一个接收线程
thread = threading.Thread(target=ComReci)
thread.start()
来完成串口数据大接收,,,然后把接收到的数据更新到Tkinter的Text控件上来显示。。

由于Tkinter本身是单线程的——网上说的我也不懂;不能在上面建立的线程中更新Tkinter线程中的控件的显示数据。

所以,我就想:自己派生一个Tkinter.Event的子类COMEvent,在串口数据接收线程中向Tkinter线程发送消息,激活Tkinter线程中COMEvent事件处理函数来更新Text控件上的数据显示。。。

但是我对Tkinter的事件处理不太懂,网上也查不出资料,所以希望这里的大侠前辈能够指点一下,给个思路。。。谢谢啦,在线等待大侠指点。。。。。。。。
...全文
592 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
iambic 2011-08-22
  • 打赏
  • 举报
回复
wxPython的也没多复杂吧,我记得也就三四行代码的事。

新版本的tk用原生的界面(我记得是前几年tcl8.5的时候吧),看起来确实比以前好看多了。
XIANXir 2011-08-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 angel_su 的回复:]

google下tkinter event_generate
[/Quote]
多谢大侠指点,已经解决了::
代码相关片段:

com_thread = threading.Thread(target=ComReci)
com_thread.start()

root.bind("<<COMRxRdy>>",lambda evt:txt1.insert("insert",Text))

def ComReci():
global Text
while True:
if COM.isOpen():
Text = COM.read(1) #read one, with timout
if Text:
n = COM.inWaiting()
if n:
Text = Text+COM.read(n)
root.event_generate("<<COMRxRdy>>",when="tail")
time.sleep(0.02)


软件运行效果:


没想到,Tkinter产生自定义事件这么简单,连继承Event都不需要,比wxPython那个例子简单不知道多少倍呀——wxPytho那个例子至今还没完全看懂。。。。

再次感谢大侠的热心指点。。。。。。。。。
angel_su 2011-08-22
  • 打赏
  • 举报
回复
google下tkinter event_generate
大三下学期期末复习: 卷 64G 的文件夹 PATH 列表 卷序列号为 364D-CC52 G:. │ ..txt │ ├─jsp课件 │ 1.ppt │ 2.ppt │ 3.ppt │ 4.ppt │ 5.ppt │ 6.ppt │ 7.ppt │ 8.ppt │ ├─matlab课件 │ 数字图像处理01.ppt │ 数字图像处理02-灰度处理.ppt │ 数字图像处理03-投影分割.ppt │ 数字图像处理04-形态学处理.ppt │ 数字图像处理05-形态学处理II.ppt │ 数字图像处理06-滤波.ppt │ 数字图像处理07-车牌处理彩色图像.ppt │ 数字图像处理08-图像梯度边缘追踪.ppt │ 数字图像处理09-模式识别.ppt │ ├─python课件 │ │ 069008-01.rar │ │ 样书.JPG │ │ 课件密码说明.doc │ │ │ ├─作业 │ │ python基础.docx │ │ │ ├─课件1 │ │ ├─第10章 网络程序设计 │ │ │ │ 第10章 网络程序设计.ppt │ │ │ │ │ │ │ └─code │ │ │ │ chatClient.py │ │ │ │ chatServer.py │ │ │ │ client.py │ │ │ │ flask_email.py │ │ │ │ flask_test.py │ │ │ │ ftpClient.py │ │ │ │ ftpServer.py │ │ │ │ getIP_MAC.py │ │ │ │ index.py │ │ │ │ index1.asp │ │ │ │ pachong.py │ │ │ │ port_scan.py │ │ │ │ receiver.py │ │ │ │ sender.py │ │ │ │ server.py │ │ │ │ sniffer.py │ │ │ │ sockMiddle.py │ │ │ │ sockMiddle_client.py │ │ │ │ sockMiddle_server.py │ │ │ │ udp_timeClient.py │ │ │ │ udp_timeServer.py │ │ │ │ │ │ │ ├─crawYichangGuishi │ │ │ │ │ result.txt │ │ │ │ │ scrapy.cfg │ │ │ │ │ │ │ │ │ └─crawYichangGuishi │ │ │ │ │ items.py │ │ │ │ │ pipelines.py │ │ │ │ │ settings.py │ │ │ │ │ __init__.py │ │ │ │ │ │ │ │ │ ├─spiders │ │ │ │ │ │ spiderYichangGuishi.py │ │ │ │ │ │ __init__.py │ │ │ │ │ │ │ │ │ │ │ └─__pycache__ │ │ │ │ │ spiderYichangGuishi.cpython-35.pyc │ │ │ │ │ __init__.cpython-35.pyc │ │ │ │ │ │ │ │ │ └─__pycache__ │ │ │ │ settings.cpython-35.pyc │

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧