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

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

问题描述

以下代码在MS Windows中正常工作(按 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()$ b $我试图在一个无窗口管理器的Debian环境中运行它(启动到控制台,运行$ code $ c
$ b $ c> startx
,它通过 .xinitrc (唯一的命令)启动脚本。



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



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

解决方案

overrideredirect 程序损失与窗口管理连接,似乎无法获取有关按键的信息,甚至无法关注。



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

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






BTW:为了测试我使用 self.root.after(5000,self.root.destroy) - 当我无法控制它5秒后杀死窗口。






编辑:



c>全屏。



在Linux程序中使用 overrideredirect 可以获得键盘事件,因此绑定不起作用,您无法关注 Entry()。但是鼠标和 Button()可以正常工作。 overrideredirect 适用于带有或不带按钮的启动屏幕。

 导入Tkinter作为tk 

类App():
def __init __(self):
self.root = tk.Tk()

#this作品

self.root.attributes(' - 全屏',True)

#这不工作

#self.root.overrideredirect (true)
#self.root.geometry(800x600 + 100 + 100)#看到控制台后面的
#self.root.after(5000,self.appexit)#杀死程序后5s

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

tk.Label(text =some text here)。grid()$ b $ = tk.Entry(self.root)
e.grid()
e.focus()#focus不适用于overrideredirect

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

self.root.mainloop()

def q_pressed(self,event):
printq_pre ssed
self.root.destroy()

def appexit(self):
printappexit
self.root.destroy()

App()


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?

解决方案

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

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

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