- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在通过 Tkinter 创建一个简单的 Python UI,我想使用 self.title
在生成回调事件时更改窗口标题。
如果我将事件绑定(bind)到按钮,或直接在 Tk 线程中调用事件处理程序,窗口标题会按预期更改。但是,我打算由单独的线程调用此事件,并且我发现在回调事件处理程序中使用 title
会导致应用程序挂起。
我在事件处理程序中执行的其他任务(例如更新标签)工作正常,因此我必须假设事件被正确调用。我已经尝试使用 wm_title
而不是 title
,但没有发现任何区别。我仔细研究了一下,发现 title
的用法没有什么奇怪的,只需用一个字符串调用它来设置标题。
这是一个复制问题的精简示例(我在 WinXP 上运行 v2.7.1,仅供引用);应用程序运行正常 10 秒(可以移动窗口、调整大小等),之后 Timer
生成事件,然后应用程序卡住。
import Tkinter
import threading
class Gui(Tkinter.Tk):
def __init__(self, parent=None):
Tkinter.Tk.__init__(self, parent)
self.title('Original Title')
self.label = Tkinter.Label(self, text='Just a Label.',
width=30, anchor='center')
self.label.grid()
self.bind('<<change_title>>', self.change_title)
timer = threading.Timer(10, self.event_generate, ['<<change_title>>'])
timer.start()
def change_title(self, event=None):
self.title('New Title')
G = Gui(None)
G.mainloop()
最佳答案
我遇到了同样的问题,当从主线程以外的线程调用 self.title() 时,UI 挂起。 Tkinter 期望所有 UI 内容都在同一个线程(主线程)中完成。
我的解决方案是让单独的线程将函数放入队列中。主线程使用 Tkinter 提供的 after(ms) 函数定期为队列提供服务。这是您的代码示例:
import Tkinter
import threading
from Queue import Queue, Empty
class Gui(Tkinter.Tk):
def __init__(self, parent=None):
Tkinter.Tk.__init__(self, parent)
self.ui_queue = Queue()
self._handle_ui_request()
self.title('Original Title')
self.label = Tkinter.Label(self, text='Just a Label.',
width=30, anchor='center')
self.label.grid()
self.bind('<<change_title>>', self.change_title)
timer = threading.Timer(1, self.event_generate, ['<<change_title>>'])
timer.start()
def change_title(self, event=None):
# Separate the function name, it's args and keyword args,
# and put it in the queue as a tuple.
ui_function = (self.title, ('New Title',), {})
self.ui_queue.put(ui_function)
def _handle_ui_request(self):
'''
Periodically services the UI queue to handles UI requests in the main thread.
'''
try:
while True:
f, a, k = self.ui_queue.get_nowait()
f(*a, **k)
except Empty:
pass
self.after(200, self._handle_ui_request)
G = Gui(None)
G.mainloop()
关于Python Tkinter : App hangs when changing window title via callback event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7157868/
我有一个非常奇怪的问题,到目前为止我无法调试......问题是......我的应用程序需要下载一些东西才能工作。因此,在 onCreate() 方法的开头,我检查是否已经下载了某些内容。如果没有,我会
我编写了一个基于尝试解决骑士之旅问题的程序。我相信我已经想出了一个合适的解决方案,一切看起来都很好。 我很好奇的一个小问题是一小段代码实现了基于展望 future 可能的方 block 的最佳着法。
我有一段很长的java代码,它使用selenium webdriver和firefox来测试我的网站。如果我不能在这里复制它,请原谅我。它有一个无限的 while 循环来重复执行其功能。这就是它应该做
def retCursor(): host = "localhost" user = "disappearedng" db = "gupan_crawling3" co
与设计师相比,我更像是一名网络程序员,但目前我的工作要求我了解更多关于网页设计的知识。对不起,如果这个问题对专业人士来说太天真了。 在这个问题上卡了几个小时,不知道该怎么办... 请到我的博客查看底部
这是我的源代码: #include using namespace std; int main() { int numBoxes, // Number of boxes
我想要标记单词组的标签,其中: 标签在词组的左边 文字是用线括起来的,标签不是 文字缩进,标签不缩进 这里有一些东西会产生错误的格式。左侧边界应用于::before 生成的文本,缩进也应用于::bef
我运行了几个 python 子进程来将数据迁移到 S3。我注意到我的 python 子进程经常下降到 0%,并且这种情况持续超过一分钟。这会显着降低迁移过程的性能。 这是子流程的图片: 子进程执行以下
编辑:其他容器正常运行。 docker run hello-world 工作正常。 我正在尝试运行最新的 nginx docker 镜像。它无限期地挂起。我已经在 2 个单独的全新安装的 ubuntu
大家好,我要脱发了。我有一个试图连接到服务器的客户端,一切似乎都很好,使用 gethostbyname() , socket() , bind() ,但是当试图 connect()它只是卡在那里,服务
我正在尝试通过驱动器映射在远程服务器上运行批处理文件,如下所示,但进程挂起...... Enable-WSManCredSSP -Role Client -DelegateComputer somes
我刚开始使用 postman 。我在执行集合运行程序时遇到了这个错误“错误:套接字挂断”。我已经阅读了一些关于套接字挂断的帖子,它提到了发送请求并且服务器端没有响应并且可能超时。如何在 Postman
我正在 http 请求函数内调用异步函数。被调用的异步函数执行另一个 http 请求,但该 http 请求抛出错误 错误:套接字挂起 代码 var http = require("http")
我之前使用 arraylist 作为结构,但在这段代码中它不起作用。由于我找不到错误,有人可以帮助我吗? (我确信这是我的错误,但 IDE 没有说什么) 流程:首先是类(class)游戏。我调用 ru
我有一个简单的套接字服务器(用于 HL7 通信)。当它在生产中运行时间较长时,套接字线程会挂起并消耗大量 CPU 时间。 这是监听器线程的相关代码(缩短): public void run() {
我想使用以下代码创建一个信号量计数器: union semun arg_assistant; int max_ass = atoi(argv[1]); printf("Num massimo di a
我正在尝试让我的模型迁移到我正在使用的 postgress 数据库,该数据库在本地托管,我确认它已正常运行。 当我运行 sequelize db:migrate 时,它说 Loaded configu
有人知道为什么 IMemAllocator:GetBuffer 的任何原因吗? (Directshow) 挂起,除了所有正在使用的样本? 我有一个使用 GMFBridge by Geraint Dav
我一直在尝试使用 Java 的 ProcessBuilder 在 Linux 中启动一个应该“长期”运行的应用程序。该程序运行的方式是启动一个命令(在本例中,我正在启动一个媒体播放应用程序),允许它运
我需要获取一个文件列表,将它们修剪到目录路径并返回一个不同的列表。在某些情况下,这可能会处理超过 500 万个文件。 我遇到了一个问题,即核心进程因 CPU 使用率 0% 而挂起,原因我无法确定。 v
我是一名优秀的程序员,十分优秀!