How to bind Tkinter destroy() to a key in Debian?

你说的曾经没有我的故事 提交于 2019-12-29 10:03:35

问题


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()

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)).

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.

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


回答1:


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 is one big window manager so it seems that overrideredirect doesn't work on that system.

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


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


EDIT:

Some (working) example with fullscreen.

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()



回答2:


If a key binding doesn't work, it is likely that the window to which the binding is associated doesn't have the keyboard focus. In your situation with no window manager, your program probably doesn't have keyboard focus.

You might try forcing it to have focus with root.focus_force(). This will sometimes give focus to a window even when the application as a whole isn't the foreground app. This is somewhat depending on the window manager, or lack thereof.




回答3:


This usually works for me:

def appexit(self, event):
    self.root.quit() # end mainloop
    self.root.destroy()


来源:https://stackoverflow.com/questions/20287019/how-to-bind-tkinter-destroy-to-a-key-in-debian

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!