Widgets attributes are a real nuisance in tkinter: there are dozens of options, and each widget accepts only a subset of them, making it difficult to remember their name and meaning (even the documentation is not always complete). I tried to semplify this situation, renaming some option in a way easier to remember, and introducing new functions to set some of them.
In tkinter you can use the config() method (or its alias configure()) to set the value of an option and the getc() method to get it. In Nictk I kept the Nictk.Misc.config() method (while configure() was removed) to set the values and renamed getc() into Nictk.Misc.get_config() (Misc is a general class inherited both by containers and internal widgets).
The Nictk.Widget.config() method has this signature:
and you can usually call it with any number of keyword arguments, for example:
The Nictk.Widget.get_config() has:
where key is the name of the requested option value (as a string), for example:
A common tedious situation is the need to repeat the same settings on a large number of widgets (this led to the introduction of ttk with its themed widgets). In Nictk I introduced the Nictk.Container.config_children() method. When creating a container (a window or a frame) you can call this method before adding child widgets, and all of them will inherit the options set in the method. Examples of the use of this method are in many example files.
This is an (incomplete) list of the most useful options, however in the tkinter docstrings there is always a list of all accepted options.
You can set many color for widgets; a color can be indicated:
These are the color attributes (with the widgets which accept them): in Nictk they has been renamed for a better coherence, but, if you want, you can continue to use the old names.
New Nictk name | Old tkinter name | Meaning | Applies to |
---|---|---|---|
abcolor | activebackground | Active background: the color of the background when the widget is in active state (the mouse is over it) This property seems to be OS dependent: it doesn't work for some widgets | Nictk.Button, Nictk.Checkbutton, Nictk.Label, Nictk.Menu, Nictk.Radiobutton, Nictk.Scale, Nictk.Scrollbar, Nictk.Spinbox |
afcolor | activeforeground | Active foreground: see above | Nictk.Button, Nictk.Checkbutton, Nictk.Label, Nictk.Menu, Nictk.Radiobutton |
bcolor | background, bg | The background color when the widget is in normal state | All widgets |
dbcolor | disabledbackground | Disabled background: the color of the background when the widget is in disabled state | Nictk.Entry |
dfcolor | disabledforeground | Disabled foreground: the color of the text when the widget is in disabled state | Nictk.Button, Nictk.Checkbutton, Nictk.Label, Nictk.Menu, Nictk.Radiobutton, Nictk.Spinbox |
fcolor | foreground, fg | Foreground: the color of the text when the widget is in normal state | Nictk.Button, Nictk.Checkbutton, Nictk.Entry, Nictk.Label, Nictk.Listbox, Nictk.Menu, Nictk.Radiobutton, Nictk.Scale, Nictk.Spinbox, Nictk.Text |
hbcolor | highlightbackground | Highlight background: the background color when the text is highlighted | All widgets, except Nictk.Menu |
hfcolor | highlightcolor | Highlight foreground: the color of the text when it is highlighted | All widgets, except Nictk.Menu |
rbcolor | readonlybackground | Readonly background: the background color when the widget is in readonly state | Nictk.Spinbox |
sbcolor | selectbackground | Select background: the background color of a selected text | Nictk.Canvas, Nictk.Entry, Nictk.Listbox, Nictk.Spinbox, Nictk.Text |
sfcolor | selectforeground | Select foreground: the text color when it is selected | Nictk.Canvas, Nictk.Entry, Nictk.Listbox, Nictk.Spinbox, Nictk.Text |
tcolor | throughcolor | Through color: the color of the guide where the cursor slides | Nictk.Scale, Nictk.Scrollbar |
These are other attributes common to many widgets and widely used:
New Nictk name | Old tkinter name | Meaning | Applies to |
---|---|---|---|
anchor | The text position inside the widget, you can choose between N, E, NW, SE, S, SW, W, NE, CENTER | Nictk.Button, Nictk.Checkbutton, Nictk.Label, Nictk.Radiobutton | |
borderwidth | borderwidth, bd | The depth of the border in pixels: default is 2 for common widgets and 0 for frames (no border). Some shapes are not drawn well if it is 1 | All widgets |
cursor | The cursor shape when the mouse is over the widget. See here for a list of shapes | All widgets | |
font | You can indicate a font for widget text. See here for a complete reference about fonts | Nictk.Button, Nictk.Checkbutton, Nictk.Entry, Nictk.Label, Nictk.Listbox, Nictk.Menu, Nictk.Radiobutton, Nictk.Scale, Nictk.Spinbox, Nictk.Text | |
justify | This option is for widgets which allow multiline text: you can choose between LEFT, RIGHT, CENTER | Nictk.Button, Nictk.Checkbutton, Nictk.Entry, Nictk.Label, Nictk.Radiobutton, Nictk.Spinbox | |
relief | The border shape of the widget. You can choose between SUNKEN, FLAT, RAISED, GROOVE, RIDGE, SOLID (see here) | All widgets | |
takefocus | Enables (with 1) or disabes (0) the widget focus when the widget is clicked or when you press the TAB key | All widgets | |
hborder | highlightthickness | Enables (with 1) or disabes (0) the dotted rectangle around the text when the widget has the focus | Nictk.Button, Nictk.Checkbutton, Nictk.Entry, Nictk.Label, Nictk.Listbox, Nictk.Radiobutton, Nictk.Spinbox, Nictk.Text |
You can see an example of the use of these attributes in the file WidgetOptions.py.
The use of these widgets is changed in Nictk with respect to tkinter:
Name | Meaning | Applies to |
---|---|---|
height, width | These are disabled in Nictk. You must give the widgets dimensions in their constructor. If you want to change them after the construction use the Nictk.Widget.resize() method. | |
command | This associates some widget event (as clicking a button or pressing RETURN in an entry) with a callback. In Nictk it has been enhanced and you can also associate a value to this; moreover you can do this in the constructor and so you need to use this option only if you want to set the callback after the widget creation. For details see Events and callbacks. | Nictk.Button, Nictk.Checkbutton, Nictk.Combobox, Nictk.Entry, Nictk.Listbox, Nictk.Menu, Nictk.Radiobutton, Nictk.Scale, Nictk.Spinbox |
state | This can switch between "normal" and "disabled" state. In Nictk is simpler to use Nictk.Widget.activate() and Nictk.Widget.deactivate() methods. The Nictk.Spinbox widget has a third option "readonly", which you can set with the Nictk.Spinbox.mode() method | Nictk.Button, Nictk.Checkbutton, Nictk.Combobox, Nictk.Entry, Nictk.Label, Nictk.Listbox, Nictk.Menu, Nictk.Radiobutton, Nictk.Scale, Nictk.Spinbox |
text | You can use this to set the text of some widget, but is simpler to use the Nictk.Widget.set_content() and Nictk.Widget.get_content() methods | Nictk.Button, Nictk.Checkbutton, Nictk.Label, Nictk.Radiobutton |
variable | In many widgets you can associate a tk.Variable object to the widget directly in the constructor. If you want to change the variable after the widget has been created you can use the Nictk.Checkbutton.set_variable() and Nictk.Radiobutton.set_variable() methods | Nictk.Checkbutton, Nictk.Radiobutton |
wraplength | This is the text wrap length in widgets which allow multiline text. Nictk sets automatically this value (even if the widget is resized). So you can only indicate 0 (text not wrapped) or non zero (default: automatic wrap). | Nictk.Button, Nictk.Checkbutton, Nictk.Label, Nictk.Radiobutton |
Some widget (as Nictk.Entry) can hold a text as their content, other (as Nictk.Label) can hold a text or an image. Moreover, many of them allows the user to associate a tkinter.Variable object (see Variable objects). This is done in tkinter by mean of the properties text, image, textvariable and variable.
In Nictk I tried to simplify widget content management in this way: in many widget constructors there is a content option in which the user can specify different content types (a string, a tkinter.StringVar, an image ...). The constructor sets it, and then you can use the Nictk.Widget.set_content() and Nictk.Widget.get_content() methods, which simplify setting and getting these options and usually make it unnecessary to use config() for them. There is also a Nictk.Widget.init_content() method: this is automatically called by the constructor when you specify the content option, and is needed only if you want to change the widget content type (i.e. to change, for example, from a string to an image).