close

Modern Tkinter

Modern Tkinter ebook

Want TkDocs to go? If you've found the material on this site useful, please consider buying this book, which is based on the Python material in the tutorial.


Mark Roseman

p.s. your purchase helps me improve this site... thank you!

 

Only $10 for e-book. pick your format:

Paperback PDF Kindle Apple Books B&N Nook Kobo

(Slightly more for paperback. Also available on other e-book platforms.)

The widget roundup provides you with a quick and easy reference to the most important features and options for each Tk widget.

Combobox

Widget Essentials

When to use: To provide the user with a set of common choices for an option (e.g. province or state), but optionally allow them to add in their own choice that isn't in the list.
Tcl:ttk::combobox .c -textvariable province 
   -values [list AB BC MB NB NL NS ...]
Ruby:$province = TkVariable.new ( initialValue );
c = Tk::Tile::Combobox.new(parent) {textvariable $province, 
   -values ['AB', 'BC', 'MB', 'NB', 'NL', 'NS', ...]}
Perl:my $c = parent->new_ttk__combobox(-textvariable => $province, 
   -values = "AB BC MB NB NL NS ...");
Python:province = StringVar()
c = ttk.Combobox(parent, textvariable=province, 
   values=('AB', 'BC', 'MB', 'NB', 'NL', 'NS', ...))
Reference:(at www.tcl.tk)

Common Options

textvariable A variable linked to the current value of the combobox; when the variable is changed, the current value of the combobox will be updated, while when the user changes the combobox, the variable will be updated.
values The list of values that appears in the list of choices presented to the user.
width The number of characters wide the combobox widget is onscreen; this does not constrain the number of characters it can actually hold.

How do I...

Get the current value? Look at the linked variable, or use the "get" method.
Use the "current" method (with no parameters) to return the 0-based index of which item in "values" has been selected (or -1 if none of the items in values).
Change the current value? Change the linked variable, or use the "set value" method.
To pick one of the items in the values list, you can also use the "current index" method.
Disable the entry? Use the "state disabled" method.
You can reenable this with "state !disabled".
Check with "instate disabled" (returns 1 if disabled, else 0).
Restrict choices to only those in the list? Use the "state readonly" method.
Allow other choices again with "state !readonly".
If the current value of the combobox is not in the list, and it is set to readonly mode, the current value will not be changed.
Tell when the combobox changes? You can bind to the "<ComboboxSelected>" virtual event.

Less Common Options

height The number of rows high that the pop-down list of values will be displayed.
postcommand A script which will be called right before displaying the pop-down list of choices. This can be used to more dynamically set the list of choices, via the values option.