12/17/2014

Enhancing Python GUI through ttk styles

This document can help you enhance your Python graphical user interface (GUI) windows by applying styles to your data-entry ttk widgets. For more information about ttk widgets, please see Tkinter (ttk) graphical user interface for Python. For more information about applying styles to ttk widgets, please see Python Software Foundation Library - ttk styling.

This document presents two example Python programs that you can download, install, and run. The first generates a window that contains ttk widgets without styles. The second generates ttk widgets with styles.

Note: Before you can download and run the example code in this document, you need to download and install Python 3 and IDLE for Python 3. IDLE is the Python integrated development environment (IDE). For details, see Getting Started with Python Programming.

Running example ttk widgets without styles
To download and run the Python code for example ttk widgets that have no styles, do the following:
  1. In a new browser window or tab, go to Ttk_Widgets_Without_Styles.
  2. Select the entire program, and then press Ctrl+C to copy the code.
  3. Start Python 3 and open a new window in IDLE.
  4. Click File, click New File, and then press Ctrl+V to paste your copied code into your new file.
  5. Save your new file with an appropriate file name, such as ttkWidgetsNoStyles.py
  6. Click Run Module to verify that the code generates a Ttk Widgets Without Styles window similar to that in Figure 1.
  7. Test the window by experimenting with its buttons and its numeric-value entry field.
Figure 1 - Example ttk widgets to which styles have not been applied.
Running example ttk widgets with styles
To download and run Python code for example ttk widgets that have styles, do the following:
  1. In a new browser window or tab, go to Ttk_Widgets_With_Styles.
  2. Select the entire program, and then press Ctrl+C to copy the code.
  3. Start Python 3 and open a new window in IDLE.
  4. Click File, click New File, and then press Ctrl+V to paste your copied code into your new file.
  5. Save your new file with an appropriate file name, such as ttkWidgetsWithStyles.py
  6. Click Run Module to verify that the code generates a Ttk Widgets With Styles window similar to that in Figure 2.
  7. Test the window by experimenting with its buttons and its numeric-value entry field.
Figure 2 - Example ttk widgets to which styles have been applied.
Comparing the example programs
To help you get started with ttk styles, the two subsections below let you compare example Python code fragments that generate ttk widgets without styles and with styles.

Note: Figures 3 and 4 show example Python code fragments as copied into word-processor text.

Generating ttk widgets without styles - The code fragment in Figure 3 generates ttk widgets for labels and buttons shown in Figure 1. This code does not define or apply styles to these widgets.
Figure 3 - Example code fragment that defines ttk widgets without styles.
Generating ttk widgets with styles - The code fragment in Figure 4 generates ttk widgets for labels and buttons shown in Figure 2. As shown in highlighted text, this code defines styles for the label and button widgets. It also applies a custom style to the Exit button.

Note: You can define a default widget color as a literal string in double quotes, such as "red." You can define a custom color as a hexadecimal string in single quotes, such as '#afd' in the Exit.Tbutton style. You can define a widget border relief as a literal string in double quotes, such as "solid," "flat", "raised", "sunken", "groove" or "ridge."
Figure 4 - Example code fragment that defines ttk widgets with styles.
The highlighted code defines the styles.


No comments :

Post a Comment