在提升tkinter帧之前,不会处理tkinter键盘中断 [英] tkinter keyboard interrupt isn't handled until tkinter frame is raised

查看:90
本文介绍了在提升tkinter帧之前,不会处理tkinter键盘中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用python + tkinter编写的GUI应用程序.在我的工作流程中,我通常从命令行启动gui,在gui中做一些事情,然后我发现自己导航到其他终端窗口来做一些工作.不可避免地,我想在某个时候关闭GUI,出于习惯,我经常只是导航到启动GUI的终端并发送KeyboardInterrupt(Ctrl-c).但是,直到我在窗口管理器中提高GUI窗口后,才收到此中断.有谁知道为什么会这样吗?如果gui是在单个函数中启动的,那么有没有简单的解决方法-可能multiprocessing?

I have a GUI application written with python+tkinter. In my workflow, I generally start the gui from the commandline, do some things in the gui and then I find myself navigating to other terminal windows to do some work. Inevitably, I want to shut down the GUI at some point, and out of habit I often just navigate to the terminal that started the GUI and send a KeyboardInterrupt (Ctrl-c). However, This interrupt is not recieved until I raise the GUI window in the Window manager. Does anyone know why this happens? If the gui is started in a single function, is there a simple workaround -- multiprocessing maybe?

推荐答案

:

我在Redhat Linux 5.0下使用Python 1.5.我想弄清楚 使用tkinter时捕获SIGINT(或Ctrl-C)的最佳方法.到 说明我遇到的问题,请执行以下操作...

I'm using Python 1.5 under Redhat Linux 5.0. I'm trying to figure out the best way to trap a SIGINT (or Ctrl-C) when using tkinter. To illustrate the problem I have, do the following ...

-在启用tkinter的情况下构建Python-1.5.

-- Build Python-1.5 with tkinter enabled.

-进入Python-1.5下的Demo/tkinter/guido目录 建立树.

-- Go into the Demo/tkinter/guido directory under the Python-1.5 build tree.

-键入"python imageview.py image-file",其中"image-file"是 可显示图像的完整路径名.

-- Type "python imageview.py image-file", where "image-file" is the full pathname of a displayable image.

-弹出图像后,请确保窗口焦点位于 由xterm窗口保存,从该窗口可以执行"python ..."命令 刚刚被调用.

-- Once the image pops up, make sure that the window focus is held by the xterm window from which the "python ..." command was just now invoked.

-按Ctrl-C.

在这一点上,什么都没有发生. Ctrl-C似乎被忽略了. 但是现在...

At this point, nothing happens. The Ctrl-C seems to be ignored. But now ...

-无需再敲击键盘上的任何键,即可设置窗口 聚焦到显示的图像窗口.

-- Without hitting any more keys on the keyboard, set the window focus to the displayed image window.

该窗口一获得焦点,Ctrl-C就会生效.

As soon as that window gets the focus, the Ctrl-C takes effect.

我的问题是这样的:有什么方法可以重组 "imageview.py"程序,以便它将响应SIGINT(Ctrl-C) 立即,而不必将窗口焦点设置为显示的 首先图片?

My question is this: is there any way to restructure the "imageview.py" program so that it will respond to SIGINT (Ctrl-C) immediately, without having to set the window focus to the displayed image first?

在此先感谢大家为我提供的帮助.

Thanks in advance for any help you folks can give me.

您看到的是由信号处理程序的处理方式引起的. 您被困在Tcl/Tk主循环中,信号处理程序仅 由Python解释器处理.一个快速的解决方法是使用 after()将虚拟函数安排为每秒调用一次 -这样可以出现及时处理您的信号 方式.

What you're seeing is caused by the way signal handlers are handled. You're stuck in the Tcl/Tk main loop, and signal handlers are only handled by the Python interpreter. A quick workaround is to use after() to schedule a dummy function to be called once a second or so -- this will make it appear that your signal is handled in a timely manner.

-吉多(Guido van Rossum)

--Guido van Rossum

这篇关于在提升tkinter帧之前,不会处理tkinter键盘中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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