next up previous
Next: Changing a widget Up: An introduction to Previous: Widgets

Creating widgets

 

A button can be created in the following way:

% button .b -text "My first button"
.b
button is the command for creating a button widget, .b is the path or the name of the button (more about paths later). The arguments -text "My first button" tell Tk to put the string "My first button" in the button. Now we tell Tk to put this button in the main window:
% pack .b
And voilà, a button appears in the main window with the text "My first button". Pressing the button won't do anything, because we haven't told Tk what to do when the button is pressed. We can do that in the following way:
% .b configure -command { puts "My first button has been pressed" }
Pressing the button will now result in printing the string "My first button has been pressed". We have bound the command puts "My first button has been pressed" to the "push button" event.



SE Praktikum
Tue Aug 13 13:33:30 MET DST 1996