Previous
Radiobutton, Checkbutton 
Next
Menubutton, Menu 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
Listbox

Widgets 4 : Listbox

listbox

A listbox is a widget that displays a list of strings, one per line. When first created, a new listbox has no elements. Elements may be added or deleted using widget commands described below.

Some Options
-listvariable VARIABLE Specifies the name of a variable. The value of the VARIABLE is a list to be displayed inside the widget; if the VARIABLE value changes then the widget will automatically update itself to reflect the new value.
-selectmode MODE Specifies one of several styles for manipulating the selection. The MODE may be arbitrary, but the default bindings expect it to be either single, browse, multiple, or extended; the default value is browse.


Some Commands
SyntaxDescriptionExample
path curselection Returns a list containing the numerical indices of all of the elements in the listbox that are currently selected. If there are no elements selected in the listbox then an empty string is returned. set sel [.lst curselection]
path delete first ?last? Deletes one or more elements of the listbox. First and last are indices specifying the first and last elements in the range to delete. If last isn't specified it defaults to first, i.e. a single element is deleted. .lst delete 5
path get first ?last? If last is omitted, returns the contents of the listbox element indicated by first, or an empty string if first refers to a non-existent element. If last is specified, the command returns a list whose elements are all of the listbox elements between first and last, inclusive. .lst get 5 end
path index index Returns the integer index value that corresponds to index. If index is end the return value is a count of the number of elements in the listbox (not the index of the last element). .lst index 5
path insert index ?element element ...? Inserts zero or more new elements in the list just before the element given by index. If index is specified as end then the new elements are added to the end of the list. Returns an empty string. .lst insert end "Me"
path size Returns a decimal string indicating the total number of elements in the listbox. set count [.lst size]
path xview This command is used to query and change the horizontal position of the information in the widget's window. Another similar command is yview .lst xview


Example

#This function will be exected when the button is pushed
proc push_button {} {
	global age occupied gender
	set name [.ent get]
	.txt insert end "$name\($gender\) is $age years old and is "
	if { $occupied == 1 } { ;#See whether he is employed
		set job_id [.lst curselection] ;#Get the no of selected jobs
		if { $job_id=={} } { ;#If there is no job
			set job "a Non worker."
		} else {
			set job [.lst get $job_id] ;#Get the name of the job
			.txt insert end "a $job."
		}
	} else {
		.txt insert end "unemployed."
	}
}

#Jobs will be activated only if occupation is enabled
proc activate_jobs {} {
	global occupied
	if { $occupied == 1 } {
		.lst configure -state normal
	} else {
		.lst configure -state disable
	}
}

#Global Variables
set age 10
set occupied 1
set gender "Male"
#GUI building
frame .frm_name
label .lab -text "Name:"
entry .ent
#Age
scale .scl -label "Age :" -orient v -digit 1 -from 10 -to 50 \
	-variable age -tickinterval 10

#Jobs
frame .frm_job
checkbutton .chk -text "Occupied" -variable occupied -command "activate_jobs"
.chk deselect
listbox .lst -selectmode single
#Adding jobs
.lst insert end "Student" "Teacher" "Clerk" "Business Man" \
	"Militry Personal" "Computer Expert" "Others"
.lst configure -state disable ;#Disable jobs

#Gender
frame .gender
label .lbl_gender -text "Sex "
radiobutton .gender.rdb_m -text "Male"   -variable gender -value "Male"
radiobutton .gender.rdb_f -text "Female" -variable gender -value "Female"
.gender.rdb_m select

button .but -text "Push Me" -command "push_button"

#Text Area
frame .textarea
text .txt -yscrollcommand ".srl_y set" -xscrollcommand ".srl_x set" \
	-width 40 -height 10
scrollbar .srl_y -command ".txt yview" -orient v
scrollbar .srl_x -command ".txt xview" -orient h

#Geometry Management
grid .frm_name -in . -row 1 -column 1 -columnspan 2
grid .lab -in .frm_name -row 1 -column 1
grid .ent -in .frm_name -row 1 -column 2
grid .scl -in . -row 2 -column 1
grid .frm_job -in . -row 2 -column 2
grid .chk -in .frm_job -row 1 -column 1 -sticky w
grid .lst -in .frm_job -row 2 -column 1
grid .gender -in . -row 3 -column 1 -columnspan 2
grid .lbl_gender -in .gender -row 1 -column 1
grid .gender.rdb_m -in .gender -row 1 -column 2
grid .gender.rdb_f -in .gender -row 1 -column 3
grid .but -in . -row 4 -column 1 -columnspan 2
grid .txt   -in .textarea -row 1 -column 1
grid .srl_y -in .textarea -row 1 -column 2 -sticky ns
grid .srl_x -in .textarea -row 2 -column 1 -sticky ew
grid .textarea -in . -row 5 -column 1 -columnspan 2

Wow! Our 'little' example is a big (and utterly pointless) program now. I am going to stop 'exampling' from now on.
This is quite complicated isn't it? Why don't you run the script and see what a beautiful script we made. Copy the above script and paste it in a file called "info.tcl" and double click the file. Voila! We are TCL/TK programmers.

Some explaining is necessary. The jobs list will be deactivated until the Occupied activated is off. If it is turned on, the list will be activated. When the button is pushed the 'push_button' function is called and details of every input field is collected and a summary is written to the 'txt' field.

Did you notice some comment started with # while others started with ;#? Use # while starting comments in new lines and use ;# if a comment is put at the end of a line. The ';' character tells that the command is finished otherwise the compiler will think that the comment is a part of the program. This is one of the most common errors made by Tcl programmers.

Previous
Radiobutton, Checkbutton 
Next
Menubutton, Menu 
blog comments powered by Disqus
Subscribe to Feed