PySimpleGUI Cookbook – #06 Theme Selector (Demo app)

Posted by


PySimpleGUI is a powerful Python library that makes it easy to create graphical user interfaces. In this tutorial, we will learn how to create a theme selector demo app using PySimpleGUI Cookbook #06.

The theme selector demo app allows users to choose from a list of pre-defined themes and apply them to the GUI. This can be useful for users who want to customize the appearance of their applications.

Let’s start by installing PySimpleGUI if you haven’t already done so. You can install it using pip:

pip install PySimpleGUI

Next, create a new Python file and import the necessary libraries:

import PySimpleGUI as sg

Now, let’s define the list of themes that we want to provide to the users. We can use the sg.theme_list() method to retrieve the list of available themes:

themes = sg.theme_list()

Next, let’s create the layout of our theme selector demo app. We will use a Column element to display the list of themes, and a Button element to apply the selected theme.

layout = [
    [sg.Text("Select a theme:")],
    [sg.Column([[sg.Button(theme, key=theme)] for theme in themes])],
    [sg.Button("Apply theme")]
]

Now, let’s create the window for our app using the sg.Window() method:

window = sg.Window("Theme Selector Demo", layout)

Next, let’s create a variable to store the selected theme:

selected_theme = sg.theme()

Now, let’s create a loop to handle events and update the GUI:

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event in themes:
        selected_theme = event
    elif event == "Apply theme":
        sg.theme(selected_theme)

window.close()

In the loop, we check for events such as closing the window, selecting a theme, and applying the selected theme. When a theme is selected, we set the selected_theme variable to the selected theme. When the "Apply theme" button is clicked, we apply the selected theme to the GUI using the sg.theme() method.

And that’s it! You have created a theme selector demo app using PySimpleGUI Cookbook #06. You can customize the app by adding more themes or additional features. Have fun exploring PySimpleGUI and creating awesome GUI applications.

0 0 votes
Article Rating

Leave a Reply

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@rubinhocruz
2 hours ago

O arquivo não esta no GITHUB

@vladimirpereira9732
2 hours ago

sg.popup_get_text(f"This is {values['-LIST-'][0]}")…..aspas duplas no início e no fim, aí funciona o f-string.
Estou tendo este retorno de erro sempre….AttributeError: module 'PySimpleGUI' has no attibute 'WIN_CLOSED'…..alguem poderia me ajudar? Grato.

2
0
Would love your thoughts, please comment.x
()
x