Tk - Button Widget



Tk button widget is a clickable widget that triggers an action. The syntax for button widget is shown below −

button buttonName options

Options

The options available for the button widget are listed below in table −

Sr.No. Syntax & Description
1

-font fontDescriptor

Used to set font for widget.

2

-height number

Used to set height for widget.

3

-command action

Sets the command action for button.

4

-text text

Sets the text for the widget.

5

-width number

Sets the width for widget.

A simple button widget is shown below −

#!/usr/bin/wish

grid [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"]

When we run the above program, we will get the following output −

Button Example

When we click the Button1, we will get the following output −

Button Example Clicked1

When we click the Button2, we will get the following output −

Button Example Clicked2
tk_basic_widgets.htm
Advertisements