tkinter 小组件默认情况下还不能响应用户的任何操作。比如单击窗口上的按钮,该按钮并不会提供任何响应。这就是因为程序没有为这些组件绑定任何 commamd 和事件进行处理。

command 选项可以绑定一个函数或方法,当用户单击小组件时,绑定的函数或方法就会被触发。 并非所有小部件中都可用 command 选项,仅限于 Button 、Scale、Menu等小部件。
import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('Canvas 画布演示')def quit(): root.destroy()button = tk.Button( root, text="退出", command=quit)button.pack(ipadx=5, ipady=5, expand=True)root.mainloop()
也可以直接调用部分命令,不使用自定义函数。
import tkinter as tkroot = tk.Tk()root.geometry('300x200+200+200')root.title('Button 按钮演示')# 此处设置按钮button = tk.Button( root, text="退出", command=root.destroy)button.pack(ipadx=5, ipady=5, expand=True)root.mainloop()当需要传递参数给回调函数时,可以使用 lambda 函数。
import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('Command 事件绑定演示')def greeting(name): print(f"Hello, {name}!")button = tk.Button( root, text="问候", command=lambda: greeting("John"))button.pack(ipadx=5, ipady=5, expand=True)root.mainloop()
Tkinter bind 用于连接在小部件中传递的事件以及事件处理程序。事件处理程序是在事件发生时调用的函数。
要将事件绑定到一个特定的小部件,使用以下构造函数:
widget.bind(event, event handler, add=None)
当发生 event 事件时, 将自动调用事件处理程序 event handler 。add 允许使用多个事件处理程序来响应同一事件。
示例
import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('Command 事件绑定演示')def on_keypress(event): print(f"{event.char} 键被按下!")entry = tk.Entry(root)entry.pack(expand=True)entry.bind("<Key>", on_keypress)root.mainloop()
以上代码中,创建了一个文本框小部件并将函数 on_keypress 绑定到按键 <Key> 事件。当在文本框中按下任何键时,会触发事件 ,输出按下的按键。
绑定多个事件import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('Command 事件绑定演示')def pressed2(event): print('鼠标中键在 x = % d, y = % d 按下!'%(event.x, event.y)) def pressed3(event): print('鼠标右键在 x = % d, y = % d 按下!'%(event.x, event.y)) def double_click(event): print('鼠标左键在 x = % d, y = % d 双击!'%(event.x, event.y)) frame = tk.Frame(root, height = 600, width = 400, bg ='white')frame.bind('<Button-2>', pressed2)frame.bind('<Button-3>', pressed3)frame.bind('<Double 1>', double_click) frame.pack(expand=True)root.mainloop()以上代码中,创建了一个框架小部件并绑定到按键 <Button-2> 、<Button-3>、<<Double 1> 三个事件,分别响应单击鼠标中键、单击鼠标右键、双击鼠标左键,输出相应按键及坐标。

下面代码示例演示将事件绑定到根窗口,并且为同一事件注册多个处理程序。当按下回车键时,调用两个事件处理程序。
import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('Command 事件绑定演示')def one(event): print('响应第一个处理程序!') def two(event): print('响应第二个处理程序!')root.bind('<Return>', one)root.bind('<Return>', two, add='+')root.mainloop()事件事件
说明
<Button>
在小部件上按下鼠标按钮。鼠标左键由事件 <Button-1> ,鼠标中键 <Button-2>,鼠标右键 <Button-3>,<Button-4> 滚轮向上滚动,<Button-5> 滚轮向下滚动。
<Motion>
按住鼠标按钮移动鼠标。鼠标左键、中键或右键分别使用 <B1-Motion>、<B2-Motion> 和 <B3-Motion>。鼠标指针的当前位置使用 event.x、event.y。
<ButtonRelease>
释放鼠标左键、中键或右键,请分别使用 <ButtonRelease-1>、<ButtonRelease-2> 和 <ButtonRelease-3>。
<Double-Button>
双击鼠标左键、中键或右键,请分别使用 <Double-Button-1>、<Double-Button-2> 和 <Double-Button-3>。
<Enter>
鼠标指针进入小部件。
<Leave>
鼠标指针离开小部件。
<FocusIn>
小部件获得焦点。
<FocusOut>
小部件失去焦点。
<Return>
用户按下了 Enter 键。可以绑定到键盘上几乎所有的键:特殊键是 Cancel(Break 键)、BackSpace、Tab、Return(Enter 键)、Shift_L(任意 Shift 键)、Control_L(任意 Control 键)、Alt_L(任意 Alt 键)、Pause、Caps_Lock、Escape、Previous(向上翻页)、Next(向下翻页)、End、Home、Left、Up、Right、Down、Print、 Insert、Delete、F1、F2、F3、F4、F5、F6、F7、F8、F9、F10、F11、F12、Num_Lock 和 Scroll_Lock。
<key>
按下任意键。
<Shift-Up>
按下向上箭头,同时按住 Shift 键。可以使用 Alt、Shift 和 Control 等前缀。
<Configure>
小组件的大小更改。
事件属性属性
说明
widget
widget 实例,而不是名称。
x、y
当前鼠标位置(以像素为单位)。
x_root、y_root
当前鼠标相对于屏幕的左上角的位置 。
char
字符代码(仅限键盘事件),以字符串形式显示。
keysym
键符号(仅限键盘事件)。
keycode
键代码(仅限键盘事件)。
num
按钮编号(仅限鼠标按钮事件)。
width, height
小组件的新大小(以像素为单位)(配置事件 only) 。
type
事件类型。
在上面的示例中,介绍了如何将事件绑定到小部件的特定实例,这称为实例级绑定。
Tkinter 还允许将事件绑定到某个小部件的所有实例。例如,可以将事件绑定到由类生成的的所有文本框:
root.bind_class('Entry', '<Control-V>', paste)
这称为类级绑定,因为将事件绑定到类而不是实例。