Title: Make a skinned form in Python, Part 4
This final post in the series about skinned forms shows how easy it is to switch between skins. There are only two new pieces of code: the code that creates the app's widgets and the code that takes action when you select a new skin.
|
Note that I have only tested this in Windows 11. It should work in Linux- and fruit-based operating systems, but I make no promises.
|
make_widgets
The following code creates the progam's three radio buttons.
def make_widgets(self):
'''Make the main app widgets.'''
self.skin_var = tk.StringVar(value='Safety')
radio1 = tk.Radiobutton(self.center_frame, text='Safety',
variable=self.skin_var, value='Safety',
command=self.skin_selected)
radio2 = tk.Radiobutton(self.center_frame, text='Pipes',
variable=self.skin_var, value='Pipes',
command=self.skin_selected)
radio3 = tk.Radiobutton(self.center_frame, text='Rivets',
variable=self.skin_var, value='Rivets',
command=self.skin_selected)
radio1.pack(side=tk.TOP, pady=(20,5))
radio2.pack(side=tk.TOP, pady=5)
radio3.pack(side=tk.TOP, pady=5)
self.load_skin(self.skin_var.get())
This code creates an IntVar to hold the selected skin. It then makes three radio buttons that use the variable. All three call self.skin_selected (shown shortly) when clicked.
The code packs the radio buttons and then calls load_skin to load the initially selected skin, which is called Safety.
skin_selected
When you click on a radio button, the following one-liner executes.
def skin_selected(self):
'''Load the skin the user selected.'''
try:
self.load_skin(self.skin_var.get())
except:
pass
This code just calls load_skin, passing it the currently selected skin's name. That's all there is to it!
Conclusion
The example comes with three skins named Safety, Pipes, and Rivets. The following images show the three skins in action.
Notice that:
- Each skin uses different images for its corners, sides, kill rectangle, and drag area (title bar).
- The images can include cyan areas that identify transparent parts of the window. The desktop behind the program shows through there and mouse events on those areas go to whatever lies behind the program.
- Different skins can place the kill rectangle in different corners.
- Different skins can use different colors for their title text.
Download the example and give it a try. With only a little work setting up the skin images, you can easily give your programs a unique appearance.
|