Next
Frame, Text, Scrollbar, Scale 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
Button, Entry, Label

Widgets 1 : Button, Entry, Label

Three things need to be said about widgets. First is the path. This I have explained earlier. The paths of all widgets must be unique and will be used when ever that widget needs to be accessed. Second is the options. Each widget has some options which can be used to configure it. This is usually done when the widget is declared, but it can be done afterward also. The final thing is commands. Each widget has some commands which also can be used to configure it or make it do some thing.

But before we begin, we need to know a little about the pack command. I have explained this earlier but just doing it one more time so that you don't have to push the back button. Pack is a geometry manager. Another geometry manager is 'grid' and I like it better - we will explore that latter. Pack is much more simpler than grid.

The line pack .hello tells the interpreter to pack the widget called ".hello".
If the command was pack .hello -in .frame, the widget .hello will be packed in another widget called .frame. In the absence of the '-in' option, the specified widget is put in the main window.


button

This will make a button. It can be configured to execute some code when pushed. This will usually refer to a function so when the button is pushed, the function will run. An button is shown using HTML below.

Some Options
-text "TEXT" TEXT will be the text displayed on the button
-command "CODE" CODE will be the code that is executed when the button is pushed

proc push_button {} {
	... whatever ...
}
button .but -text "Push Me" -command "push_button"
pack .but

entry

An entry is a widget that displays a one-line text string and allows the user to input and edit text in it. When an entry has the input focus it displays an insertion cursor to indicate where new characters will be inserted. An entry element is shown using HTML.

Some Options
-width NUMBERWidth of the input field. NUMBER should be an integer.
-textvariable VARIABLEThe contents of the variable VARIABLE will be displayed in the widget. If the text in the widget is edited, the variable will be edited automatically. VARIABLE should be given without a preceding '$' sign.
-state STATE The state of the input field. It can be normal, disabled, or readonly. If it is readonly the text can't be edited.
Some Commands
SyntaxDescriptionExample
path get The text inside input field can be taken by this command set name [.ent get]
path delete FIRST ?LAST? Delete one or more elements of the entry. FIRST is the index of the first character to delete, and LAST is the index of the character just after the last one to delete. If last isn't specified it defaults to FIRST+1, i.e. a single character is deleted. This command returns an empty string. .ent delete 0 end
path insert index STRING Insert the characters of STRING just before the character indicated by index. Index is 0 for the first character. The word "end" can be used for the last character .ent insert end "Hello"

Example

proc push_button {} {
	.ent insert end "Hello"
}
entry .ent
button .but -text "Push Me" -command "push_button"
pack .ent
pack .but

label

This widget display text messages.

Example

proc push_button {} {
	.ent insert 0 "Hello "
}
label .lab -text "Enter name:"
entry .ent
button .but -text "Push Me" -command "push_button"
pack .lab
pack .ent
pack .but
Next
Frame, Text, Scrollbar, Scale 
blog comments powered by Disqus
Subscribe to Feed