用python tkinter 做界面时,怎么实现键盘按下回车键后,触发某个button按钮?

准备用python 的 tkinter 库 来做一个界面。
界面中 有多个文本输入框,暂定为text1,text2,....等等。
另外还有多个button按钮,暂定为button1,button2....等等。

现在想实现:
在text1中输入一段字符后,回车,就相当于点击了button1按钮。
也就是说在text1中回车后,就触发了button1的点击操作。 这样就不需要在text1中输入字符后,再手动去鼠标点击button1按钮。

请问怎么实现?
谢谢!

你好,下面是一个例子:不过你需要用鼠标点击一下那个click me的button,然后回车就是相当于点击那个button了。
import tkinter as tk
root = tk.Tk()
root.geometry("300x200")
def func(event):
print("You hit return.")
def onclick(event):
print("You clicked the button")
root.bind('<Return>', onclick)
button = tk.Button(root, text="click me")
button.bind('<Button-1>', onclick)
button.pack()
root.mainloop()
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-12-20
import tkinter as tk

root = tk.Tk()
root.geometry("300x200")

def func(event):
    print("You hit return.")
    
def onclick(event):
    print("You clicked the button")
    
root.bind('<Return>', onclick)

button = tk.Button(root, text="click me")
button.pack()

button.bind('<Button-1>', onclick)

root.mainloop()


不换行没法儿读, 基本上就是俩event绑定一个事件函数

相似回答