将Win_L与’t’绑定在tkinter中。

huangapple go评论58阅读模式
英文:

bind Win_L with 'd' in tkinter

问题

from tkinter import *

def quit_1(event):
    print("你按下了 Win_L 键")
    root.quit()
    
def quit_2(event):
    print("你按下了 Win_L+D 键")
    root.quit()

root = Tk()
root.bind('<Win_L>', quit_1)     # 正常工作
root.bind('<Win_L-D>', quit_2)   # 无法正常工作
root.mainloop()
英文:
from tkinter import *

def quit_1(event):
    print(&quot;you pressed Win_L&quot;)
    root.quit()
def quit_2(event):
    print(&quot;you pressed Win_L+D&quot;)
    root.quit()

root = Tk()
root.bind(&#39;&lt;Win_L&gt;&#39;, quit_1)     #working
root.bind(&#39;&lt;Win_L-D&gt;&#39;, quit_2)   #not working
root.mainloop()

How I bind Win_L + D event to a funcction

commenting line
root.bind('Win_L-D',quit_2) runs the program

答案1

得分: 2

为了将 Win_L + D 组合键绑定到 quit_2 函数,您需要修改绑定方法中的事件字符串。不要使用 '<Win_L-D>',而是使用 '<Win_L>d>'

root.bind('<Win_L>', quit_1)
root.bind('<Win_L>d>', quit_2)

这是因为 Win_L 是一个修改键,不能与其他键组合使用。当您按下 Win_L + D 时,Windows 操作系统会生成组合键的新键码。在 Tkinter 中,这个键码由 d(d 键)表示,因此您需要在事件字符串中使用它。
通过这个更改,按下 Win_L + D 组合键将在控制台打印 "you pressed Win_L+D" 并退出根窗口。

英文:

To bind the Win_L + D key combination to the quit_2 function, you need to modify the event string in the bind method. Instead of using &#39;&lt;Win_L-D&gt;&#39;, you need to use &#39;&lt;Win_L&gt;d&#39;:

root.bind(&#39;&lt;Win_L&gt;&#39;, quit_1)
root.bind(&#39;&lt;Win_L&gt;d&#39;, quit_2)

This is because Win_L is a modifier key and cannot be used in combination with other keys. When you press Win_L + D, the Windows operating system generates a new key code for the combination. In Tkinter, this code is represented by d (the d key), so you need to use it in the event string.
With this change, pressing the Win_L + D key combination will print "you pressed Win_L+D" to the console and quit the root window.

huangapple
  • 本文由 发表于 2023年2月16日 12:16:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定