Tk按钮小部件是可触发操作的可单击小部件。按钮小部件的语法如下所示-
button buttonName options
Button - 参数
下表中列出了按钮小部件可用的选项-
| Sr.No. | Syntax & Remark |
|---|---|
| 1 |
-font fontDescriptor 用于设置小部件的字体。 |
| 2 |
height number 用于设置小部件的高度。 |
| 3 |
-command action 设置按钮的command action。 |
| 4 |
-text text 设置小部件的文本。 |
| 5 |
width number 设置小部件的宽度。 |
一个简单的按钮小部件如下所示-
#!/usr/bin/wishgrid [label .myLabel -text "Click the buttons" -textvariable labelText] grid [button .myButton1 -text "Button 1" -font {Helvetica -18 bold} -height 5 -width 10 -command "set labelText clicked_top_btn"] grid [button .myButton2 -text "Button 2" -font {Helvetica -18 bold} -height 5 -width 10 -command "set labelText clicked_bottom_btn"]
当无涯教程运行上面的程序时,将获得以下输出-

单击Button1时,将获得以下输出-

单击Button2时,将获得以下输出-