您可以使用@martineau 在评论中指出的内容检查是否在 tkinter 中按下了修饰键,如下所示:
import tkinter as tk
from tkinter.scrolledtext import ScrolledText
def test(event):
# If you want to get the widget that fired the event, use this:
widget = event.widget
# Check if the shift key is pressed when the event is fired
shift_pressed = bool(event.state & 0x0001)
if shift_pressed:
...
else:
print("Shift isn't pressed.")
root = tk.Tk()
text_widget = ScrolledText(root)
text_widget.bind("<Return>", test)
text_widget.pack()
root.mainloop()
event.state
是一个整数,用于保存触发事件时按下的不同键组合的标志。检查事件& 0x0001
触发时是否按下了 shift 键。