如何将 Tkinter destroy() 绑定到 Debian 中的一个键? [英] How to bind Tkinter destroy() to a key in Debian?

查看:42
本文介绍了如何将 Tkinter destroy() 绑定到 Debian 中的一个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在 MS Windows 中正常工作(按 q 时脚本退出):

The following code works correctly in MS Windows (the script exits when pressing q):

import Tkinter as tk

class App():
    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry("{0}x{1}+0+0".format(
            self.root.winfo_screenwidth(), self.root.winfo_screenheight())
        )
        self.root.overrideredirect(True)
        tk.Label(text="some text here").grid()
        self.root.bind('q', self.appexit)
        self.root.mainloop()

    def appexit(self, event):
        self.root.destroy()

App()

我尝试在无窗口管理器"的 Debian 环境中运行它(启动到控制台,运行 startx,它通过 .xinitrc 启动脚本(唯一的命令在那里)).

I tried to run it in a "window manager-less" Debian environment (boot to console, run startx, which launches the script via .xinitrc (the only command there)).

脚本按预期启动,但按 q 不会执行任何操作(我希望 X 关闭并返回文本控制台).我后来尝试在 mainloop() 之前添加以防万一 self.root.focus() 但它没有帮助.

The script starts as expected but pressing q does not do anything (I was expecting X to close and return to the text console). I later tried to add just in case self.root.focus() before the mainloop() but it did not help.

MS Windows 和 Debian 环境之间出现这种不同行为的原因可能是什么?

What could be the reason of this different behavior between the MS Windows and Debian environment?

推荐答案

With overrideredirect 程序失去了与窗口管理的连接,因此它似乎无法获取有关按下的按键的信息,甚至无法聚焦.

With overrideredirect program loses connection with window manage so it seems that it can't get information about pressed keys and even it can't be focused.

MS Windows 是一个大窗口管理器,所以 overrideredirect 似乎在那个系统上不起作用.

MS Windows is one big window manager so it seems that overrideredirect doesn't work on that system.

也许你可以使用 self.root.attributes('-fullscreen', True) 代替 self.root.overrideredirect(True)

Maybe you could use self.root.attributes('-fullscreen', True) in place of self.root.overrideredirect(True)

顺便说一句:为了测试我使用 self.root.after(5000, self.root.destroy) - 当我无法控制它时在 5 秒后杀死窗口.

BTW: for testing I use self.root.after(5000, self.root.destroy) - to kill window after 5s when I can't control it.

fullscreen 的一些(工作)示例.

Some (working) example with fullscreen.

在 Linux 程序上使用 overrideredirect 可以获得键盘事件,因此绑定不起作用,并且您无法聚焦 Entry().但是鼠标和 Button() 可以工作.overrideredirect 适用于带或不带按钮的启动画面".

With overrideredirect on Linux program can get keyboard events so binding doesn't work, and you can't focus Entry(). But mouse and Button() works. overrideredirect is good for "splash screen" with or without buttons.

import Tkinter as tk

class App():
    def __init__(self):
        self.root = tk.Tk()

        # this works

        self.root.attributes('-fullscreen', True)

        # this doesn't work

        #self.root.overrideredirect(True)
        #self.root.geometry("800x600+100+100") # to see console behind
        #self.root.after(5000, self.appexit) # to kill program after 5s

        self.root.bind('q', self.q_pressed)

        tk.Label(text="some text here").grid()
        e = tk.Entry(self.root)
        e.grid()
        e.focus() # focus doesn't work with overrideredirect 

        tk.Button(self.root, text='Quit', command=self.appexit).grid()

        self.root.mainloop()

    def q_pressed(self, event):
        print "q_pressed"
        self.root.destroy()

    def appexit(self):
        print "appexit"
        self.root.destroy()

App()

这篇关于如何将 Tkinter destroy() 绑定到 Debian 中的一个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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