Several widgets allow to enter text or numerical information in a single field.
- the Entry widgets allows to type text
- the Combobox allows to either type text or select text from a drop-down list
- the Spinbox allows to select from a numerical range or from a list
- the Scale allows to use a slider to choose a value
entry0.py<entry0.py>
Since these widgets are very similar, we treat them in the same section.
An entry widget presents the user an empty field where he can enter a text value.
Each ttk.Entry
object has these options:
- parent - the parent object
- textvariable - the text variable to hold the entered string
- width - the numbers of characters
- show - to indicate
*
for passwords
The Entry widget does not have a text
or image
option. You have to use an additional label widget instead.
entry1.py
entry1.py<entry1.py>
It's time now to define a new and better Entry
class which can do everything in one line:
Entry('First name:', 'print(self.var.get())')
This new class has the attributes:
- label - to automatically add label in front of the entry field
- cmd - to execute a command string when hitting the Return key
- val - a default value
The command function evaluates the expression entered and displays the result in the following label widget:
Entry('Enter expression', 'App.res["text"] = eval(self.var.get())')
App.res = Label('Result')
entry2.py
entry2.py<entry2.py>
Now let's see how this class is defined
tklib.py
A combobox combines an entry widget with a list of choices. The user can either select text from a drop-down menu or write his own text.
The first combobox allows to also enter your own text, while the second one is restricted to chose an item from the drop-down menu by setting state='readonly'
.
The Combobox class has the options
- parent - for the parent object
- textvariable - for the variable which stores the value
- values - for the items list
- state - to indicate
readonly
combo1.py
combo1.py<combo1.py>
A combobox combines a list of choices with an entry. The user can select from the list, but he can also enter directly a value.
The items list can be a:
- semicolon-separated string
- integer list: [2005, 2006, 2007]
- list expression (list(range10))
combo2.py
combo2.py<combo2.py>
How is this new class defined ?
tklib.py
combo3.py
combo3.py<combo3.py>
The items list can be a:
- semicolon-separated string
- tuple
- list
- list expression (list(range10))
The command function can be either a string to execute or a function.
combo4.py
combo4.py<combo4.py>
A spinbox widget is an entry widget with built-in up and down buttons that are used to either modify a numeric value or to select among a set of values. The widget implements all the features of the entry widget.
spinbox1.py
spinbox1.py<spinbox1.py>
spinbox2.py
spinbox2.py<spinbox2.py>
A scale widget provides a way for users to choose a numeric value through direct manipulation.
scale2.py
scale2.py<scale2.py>
The four classes Entry
, Combobox
, Spinbox
and Scale
have two common parts:
- adding an optional label in front of the widget
- adding a callback function
This two functions can be placed in a specal class called EntryMixin
, which serves as second parent class for the 4 entry classes.
tklib.py
This allows to make the Entry
class much shorter.
tklib.py
The other class definitions are as follows:
tklib.py
tklib.py
tklib.py