Style Commands#

To manipulate the appearance of a widget - changing its style - we use the class Style() and one or more of its commands. Using common style changes on several widgets we can produce a theme.

The table 01style_commands.csv has a summary of all the Style() commands,

Table 01style_commands.csv#

Show/Hide Table 01style_commands.csv

Command

Effect

Style

class used to manipulate the style database

configure(style, query_opt=None, **kw)

query or set the default value of specified option(s)

element_create(_elementname,etype, *args, **kw)

creates a new element in the current theme

element_names()

lists elements defined in current theme

element_options(elementname)

lists the options of elementname

layout(style, layoutspec=None)

defines the widget layout for a given style

lookup(style,option,state=None,default=None)

returns value of value specified by option in style

map(style, query_opt=None, **kw)

query or set dynamic states

theme_create(themename, parent=None, settings=None)

create a new theme

theme_names()

lists all known themes

theme_settings(themename, settings)

temporarily sets the current theme to specified settings

theme_use(themename=None)

if themename is not given returns current theme - otherwise it sets the theme


Note

the variables quoted here are local variables, so style may be a reference to a widget class or cross-reference

Buttons in tkinter and ttk#

Warning

Image Quality

Some IDEs will not show the images created in tkinter / ttk at the same resolution or quality if you are using an ultra high definition monitor. Standard monitors should be the same no matter which IDE is used. If there is a difference run the Python script from the command line, Idle or use PyScripter.

Using buttons compare the two different types of widgets, use the script 01two_buttons.py - found in the examples directory. You should see 4 buttons, the upper two buttons are standard tkinter, whilst the lower two are ttk buttons.

Comparing Client Interaction on Buttons#

Default ttk Buttons

Windows ttk Buttons

d2

v2

c2

All four buttons are grey but the tkinter buttons are paler. Move the cursor over all four buttons. The two ttk buttons lighten but the tkinter buttons do not react. Click on all four buttons, all four appear to be depressed, but the two ttk buttons will have a broken line showing which one of the two buttons was last activated.

Show/Hide Video two_buttons

Buttons, in common with several other widgets, have what we call states, for example when a cursor passes over the widget its state changes to active, so we have just seen how the ttk button's state together with the theme used affects its appearance.

Script 01two_buttons.py#

To view or hide the code just click on the arrow.

Show/Hide Code 01two_buttons.py

 1""" Comparing the Button widgets 
 2
 3Compare different methods of running python.
 4"""
 5from tkinter.ttk import Style, Label
 6from tkinter import Tk
 7from tkinter import Button as origbutton
 8from tkinter.ttk import Button as tilebutton
 9
10root=Tk()
11s = Style()
12# using the ttk default scheme 
13s.theme_use('default') 
14Label(root,text='Move your mouse over each of the buttons below,\n \
15      then left click on each of them').pack()
16origbutton(root,text='original').pack()
17origbutton(root,text='2nd original').pack()
18tilebutton(root,text='ttk themed').pack()
19tilebutton(root,text='2nd themed').pack()
20
21root.mainloop()

If we had left out the line:

13 s.theme_use('default')

and we were running either a Windows or Mac system then we would have seen blue ttk buttons because both operating systems have their own OS specific themes.

By using a theme many ttk widgets react by default without any special input. This is in contrast to the original tkinter widgets which have to be individually programmed.