后台进程和tkinter [英] Background process and tkinter

查看:41
本文介绍了后台进程和tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找有关从哪里开始的帮助,对 Python 不太好.我想做的是将 tkinter 用于 gui 界面,但我需要能够处理收到的数据并随着信息的变化更新标签小部件.我已经准备好让我的程序的通信部分在 shell 中正常工作,但是当我尝试将它绑定到 tkinter 时,它会在生成接口后立即停止处理.任何人都有一个简单的代码供我修改以满足我的需要或指向某个地方的参考示例.到目前为止,我花了几天时间尝试不同的选择,但我仍然没有找到有效的方法.

Looking for help on where to start with this, not too good with Python. What I trying to do is use tkinter for a gui interface but i need to be able to process recieved data and update labels widgets as information changes. I all ready have the communication portion of my program working fine in the shell but when I try to tie it to tkinter it will stop processing as soon as the interface is generated. Anyone have a simple code for me to modify to my needs or point me to a reference example somewhere. Spent days so far trying different options and I still have yet to find something that works.

感谢您的帮助

推荐答案

将您的工作程序转换为可以在 tkinter UI(例如按钮或其他小部件)中注册为回调的函数,也就是说,使其成为事件驱动的,然后,对于后台处理,使用 after 小部件方法注册一些函数.root.mainloop() 永远不会返回(仅在 UI 关闭时),将其用作最后一条指令.

Convert your working program into functions that you can register as callbacks in the tkinter UI (say buttons, or other widgets), that is, make it event-driven, and then, for background processing register some of the functions with the after widget method. The root.mainloop() will never return (only on UI close), use it as the last instruction.

因此,您不能只在自顶向下的结构中编写逻辑,并希望它与 UI 配合良好.主循环将永久循环,并会根据从用户接收到的事件或在一段时间后使用 after 注册运行的回调调用代码中的特定函数.

So you can't just write your logic in a top-down structure, and hope that it will work well with the UI. The mainloop will be permanently looping, and will call specific funtions in your code, as appropriate to the received events from the user, or to callbacks you registered to run after some time with after.

请参阅此处了解after部分

查看此处,了解如何构建 tkinter 程序.它应该有足够的信息和链接供您学习和学习如何以正确的方式去做.

Take a look here for structuring tkinter programs. It should have enough info and links for you to study and learn how to do it in a right way.

这篇关于后台进程和tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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