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.
O arquivo não esta no GITHUB
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.