Tkinter<返回>按键释放事件

发布于 2024-12-14 21:59:21 字数 161 浏览 1 评论 0原文

有没有办法在按键释放时调用 事件,而不是按下?

如果您使用 ,则 event.char 对于任何特殊键都是空白,而不仅仅是返回。

Is there a way to make the <Return> event call on the key release, instead of press?

If you use <KeyRelease>, then event.char is blank for any special key, not just return.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰沐子 2024-12-21 21:59:21

您可以通过绑定到 显式设置释放返回键的绑定。

例如:

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.text = tk.Text(self)
        self.text.pack()
        self.text.bind("<KeyRelease-Return>", self.on_return_release)

    def on_return_release(self, event):
        self.text.insert("end", "boink! ")

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

如果您不希望在按下返回键时发生任何事情,您可以删除默认绑定,或者创建您自己的返回“break”的绑定

You can explicitly set a binding on the release of the return key by binding to <KeyRelease-Return>.

For example:

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.text = tk.Text(self)
        self.text.pack()
        self.text.bind("<KeyRelease-Return>", self.on_return_release)

    def on_return_release(self, event):
        self.text.insert("end", "boink! ")

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

If you don't want anything to happen on a press of the return key you can remove the default binding, or create your own binding which returns "break"

吹泡泡o 2024-12-21 21:59:21

您可以检查 event.keysym=='Return'

class App(object):
    def __init__(self, master, **kwargs):
        self.master=master
        master.bind('<KeyRelease>',self.release)
    def release(self,event):
        if event.keysym=='Return':
            print('Return Release')
root=tk.Tk()
app=App(root)
root.mainloop()

请注意,如果您有自动键重复,那么即使您没有实际释放按键,也会出现 KeyRelease 事件。


要在 Xwindows 下关闭自动重复:

import Xlib.display as display
import Xlib.X as X
import Tkinter as tk
import atexit

@atexit.register
def autorepeat():
    d=display.Display()    
    d.change_keyboard_control(auto_repeat_mode=X.AutoRepeatModeOn)
    x=d.get_keyboard_control()    

class App(object):
    def __init__(self, master, **kwargs):
        self.master=master
        master.bind('<KeyRelease>',self.release)
    def release(self,event):
        if event.keysym=='Return':
            print('Return Release')

d=display.Display()
d.change_keyboard_control(auto_repeat_mode=X.AutoRepeatModeOff)
x=d.get_keyboard_control()
root=tk.Tk()
app=App(root)
root.mainloop()

You could check that event.keysym=='Return':

class App(object):
    def __init__(self, master, **kwargs):
        self.master=master
        master.bind('<KeyRelease>',self.release)
    def release(self,event):
        if event.keysym=='Return':
            print('Return Release')
root=tk.Tk()
app=App(root)
root.mainloop()

Note that if you have autokey repeat, then there will be KeyRelease events even if you do not physically release the key.


To turn off autorepeat under Xwindows:

import Xlib.display as display
import Xlib.X as X
import Tkinter as tk
import atexit

@atexit.register
def autorepeat():
    d=display.Display()    
    d.change_keyboard_control(auto_repeat_mode=X.AutoRepeatModeOn)
    x=d.get_keyboard_control()    

class App(object):
    def __init__(self, master, **kwargs):
        self.master=master
        master.bind('<KeyRelease>',self.release)
    def release(self,event):
        if event.keysym=='Return':
            print('Return Release')

d=display.Display()
d.change_keyboard_control(auto_repeat_mode=X.AutoRepeatModeOff)
x=d.get_keyboard_control()
root=tk.Tk()
app=App(root)
root.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文