Python Tkinter 的 overrideredirect 无法接收按键输入(Linux)

4 投票
2 回答
4935 浏览
提问于 2025-04-16 16:59

我有一个用Python和tkinter做的应用程序,我想让它全屏显示。当我取消注释overrideredirect这个选项时,窗口管理器(Gnome,Linux)就无法再把键盘输入传递给这个应用了。

(代码片段,python)

# make it cover the entire screen
w, h = master.winfo_screenwidth(), master.winfo_screenheight()
self.root.geometry("%dx%d+0+0" % (w, h))
self.root.focus_set() # <-- move focus to this widget
self.root.bind('<Escape>', self.root.quit())
#self.root.overrideredirect(True)

我发现了一个叫window::or的Tcl/Tk包,听说它可以解决这个问题。我该怎么安装它?我能在我的Python应用里使用它吗?

http://www.binarism.com/tk/window/or/

http://www.binarism.com/tk/window-or-0.1.1.tgz

2 个回答

0

当你进行绑定时,可能想要使用可调用的 self.root.quit 而不是 self.root.quit(),这样可以避免直接调用这个函数。当你按下 Escape 键时,这个可调用的函数会被调用(我知道,我知道),并且会带有一个事件参数。如果 self.root.quit() 不接受任何参数,你可以使用 lambda 表达式:self.root.bind('<Escape>',lambda e:self.root.quit())

4

这个方法适用于你想要使用overrideredirect来实现全屏的情况,这种情况是比较常见的:

#self.root.overrideredirect(1)
self.root.attributes('-fullscreen', True)

撰写回答