如何将键的组合绑定到一个方法?
How to bind a combination of keys to a method?
如何将 Ctrl + Shift + S 绑定到方法?基于 this 我尝试了下面的代码但它不起作用(打印语句不是 运行):
import tkinter as tk
def key(event=None):
print("It works!")
root = tk.Tk()
frame = tk.Frame(root, width=100, height=100)
frame.focus_set()
frame.bind('<Control-Shift-s>', key)
frame.pack()
root.mainloop()
此外,如果可能,我还想知道有关如何绑定组合键的一般答案。
当您按住 Shift 时,s 变为 S。
因此,您的绑定永远不会触发。要解决此问题,您只需将绑定更改为:
frame.bind('<Control-Shift-S>', key)
如何将 Ctrl + Shift + S 绑定到方法?基于 this 我尝试了下面的代码但它不起作用(打印语句不是 运行):
import tkinter as tk
def key(event=None):
print("It works!")
root = tk.Tk()
frame = tk.Frame(root, width=100, height=100)
frame.focus_set()
frame.bind('<Control-Shift-s>', key)
frame.pack()
root.mainloop()
此外,如果可能,我还想知道有关如何绑定组合键的一般答案。
当您按住 Shift 时,s 变为 S。
因此,您的绑定永远不会触发。要解决此问题,您只需将绑定更改为:
frame.bind('<Control-Shift-S>', key)