一、窗口上敲键盘触发事件(以Enter键为例)
二、点击窗口按钮触发事件(以鼠标左键双击为例)
代码:
import tkinter as tk root = tk.Tk() root.geometry("300x200") l1 = tk.Label(root, text="这是一个测试", font=("黑体", 10)) # 标签
l1.pack() # 指定包管理器放置组件
ask_text = tk.Entry() # 创建文本框
ask_text.pack() def onclick(event): print(ask_text.get()) #一、窗口上敲键盘触发事件(以Enter键为例)
root.bind('<Return>', onclick) #二、点击窗口按钮触发事件(以鼠标左键单击为例)
button = tk.Button(root, text="确定") button.bind('<Button-1>', onclick) button.pack() root.mainloop()
效果:
更多资料:
(46条消息) python | tkinter(六) bind事件信息_墨色幽灵的博客-CSDN博客
(46条消息) Python笔记之Tkinter(Key键盘事件)_潇洒哥的运维之道-CSDN博客_python键盘触发事件