Change theme dynamically on the fly
python - PySimpleGUI theme updates? - Stack Overflow
python - Themes not showing up in PySimpleGUI - Stack Overflow
python - How do I change background color in pysimplegui? - Stack Overflow
Videos
The idea of using "Look and Feel" settings goes way back with PySimpleGUI. You may have seen the green and tan colors windows from time to time. It's a feature that wasn't put into very many of the demo programs.
Because the demos are often the starting point for projects, it made sense to begin to spice things up a little and add color right from the start. Not everyone will be pleased with the color selection. You can always remove the call entirely (it's 1 line of code), or choose another one. Point is they are coming to a screen near you.
Previously you needed to specify the exact name of the Theme. Now it's a "Fuzzy" match. "Grey Dark 2" or "DarkGray2" both work. The original 28 Themes are sill available using the old names. They are also available using a new naming convention. The new convention is:
<"Dark" | "Light"> <Color> [#]
Where color can be: Black, Blue, Green, Teal, Brown, Yellow, Grey, Purple
Not all combinations are represented and now every combination has the same number of choices as others. For example there are 9 Dark Blue themes and 1 Light Yellow.
You can get a text list of the choices by calling:
import PySimpleGUI as sg sg.list_of_look_and_feel_values()
You will also get this text list on your console if you pass in a value that doesn't match a valid Theme.
You can also get a visual dump of the choices by calling:
sg.preview_all_look_and_feel_themes()
With the straight PySimpleGUI port (tkinter), the preview looks like this:
You can use these on the Qt and Web ports as well. The same values work and you can also make the same preview call. for the PySimpleGUIWeb port, the preview looks like this:
Putting these color themes to use is as simple as adding this line of code to your program, any place prior to creating your layout or calling a popup:
sg.change_look_and_feel('Theme Name Here') Note that this possibly a first step in creating a nice looking window. It is merely settings up some colors for you. There is more you can do to polish your window should you wish. The idea here is for people to take a break from creating and looking at gray windows all the time, especially when starting out.
If your program only has popup calls, that's OK, changing the Look and Feel changes the colors for all future windows (until another change is made).
If you have additional color scheme ideas, there is a defined format so that you can add your own by adding a dictionary entry into the Theme table.
You can change the background color by specifying a Hex Color Code in the following argument under layout:
background_color='#DAE0E6'
You can use a Color Picker like this one https://htmlcolorcodes.com/color-picker/ to get your color
You can also use:
window = sg.Window('Virus Simulation', layout, background_color='hex_color_code')
To change the color of a window object
I'm a tad confused in 2 ways.
1 - I don't see a call to sg.theme() in your code, so I don't know where it was in the code. WHERE it is placed matters. Always place the theme as early as possible, definitely before making your layout.
2 - I don't know what "it isn't working this time means". Again, it's more of needing to see a complete example to get it.
The sample code in the question that you said normally works was weirdly formatted so something must have been scrambled.
The question shows: sg.theme="color
But as Jason has pointed out, the value passed to sg.theme() is more than a color. The "Theme Name Formula" is described in the main PySimpleGUI documentation here - https://pysimplegui.readthedocs.io/en/latest/#theme-name-formula. In case there's a problem getting to that section, here's what it says:
Theme Name Formula
Themes names that you specify can be "fuzzy". The text does not have to match exactly what you see printed. For example "Dark Blue 3" and "DarkBlue3" and "dark blue 3" all work.
One way to quickly determine the best setting for your window is to simply display your window using a lot of different themes. Add the line of code to set the theme - theme('Dark Green 1'), run your code, see if you like it, if not, change the theme string to 'Dark Green 2' and try again. Repeat until you find something you like.
The "Formula" for the string is:
Dark Color #
or
Light Color #
Color can be Blue, Green, Black, Gray, Purple, Brown, Teal, Red. The # is optional or can be from 1 to XX. Some colors have a lot of choices. There are 13 "Light Brown" choices for example.
If you want to only change the background color of your theme, then you can use individual color names or hex values. sg.theme_background_color('#FF0000') or sg.theme_background_color('red') will set the background color to red.
Hope that helps with themes.
Nice work on using the PSG coding conventions. Looking at your code was effortless as a result. Zero guesswork as to what I was seeing. Great to see and it helps in numerous ways when you use them.
A new demo was posted showing how to do this on tie PySimpleGUI GitHub. Look for Demo_Theme_Change_Your_Windows_Theme.py
Here's the source from it:
import PySimpleGUI as sg
"""
Demo - Changing your window's theme at runtime
* Create your window using a "window create function"
* When your window's theme changes, close the window, call the "window create function"
Copyright 2021 PySimpleGUI
"""
# ------------------- Create the window -------------------
def make_window(theme=None):
if theme:
sg.theme(theme)
# ----- Layout & Window Create -----
layout = [[sg.T('This is your layout')],
[sg.Button('Ok'), sg.Button('Change Theme'), sg.Button('Exit')]]
return sg.Window('Pattern for changing theme', layout)
# ------------------- Main Program and Event Loop -------------------
def main():
window = make_window()
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED or event == 'Exit':
break
if event == 'Change Theme': # Theme button clicked, so get new theme and restart window
event, values = sg.Window('Choose Theme',
[[sg.Combo(sg.theme_list(), readonly=True, k='-THEME LIST-'), sg.OK(), sg.Cancel()]]
).read(close=True)
if event == 'OK':
# ---- Switch to your new theme! ---- IMPORTANT PART OF THE PROGRA<
window.close()
window = make_window(values['-THEME LIST-'])
window.close()
if __name__ == '__main__':
main()
An example that allows you to select a new theme from a theme browser showing all themes -no combo selection is needed- and updates a window by closing and recreating it.
Here is the source code:
import PySimpleGUI as sg
selected_theme = 'LightGreen'
sg.theme(selected_theme)
theme_list = sg.theme_list()
theme_list.sort()
NUM_COLUMNS = 8
theme_list_size = len(theme_list)
num_rows = int(theme_list_size/NUM_COLUMNS)
def create_theme_box(theme_index):
sg.theme(theme_list[theme_index])
return sg.Column(
[
[sg.Text('' + theme_list[theme_index], size=(16,1))] +
[sg.Button('Select', k=str(theme_index)),] ,
]
)
def create_theme_example(theme):
sg.theme(theme)
theme_frame = [
[sg.Checkbox('Checkbox')] +
[sg.Slider(range=(0, 100), orientation='horizontal', size=(20, None), default_value=50)] +
[sg.Button('Button')] +
[sg.Text(' InputText:'), sg.InputText()],
]
theme_example_layout = [
[sg.Frame(selected_theme, theme_frame)],
]
return sg.Window('Theme Example', theme_example_layout, resizable=True, finalize=True, location=(100,50))
theme_browser_layout = [
[
[create_theme_box(column+NUM_COLUMNS*row) for column in range(0, NUM_COLUMNS)] for row in range(0, num_rows)
],
[
create_theme_box(column + int(theme_list_size/NUM_COLUMNS)*NUM_COLUMNS) for column in range(0, (theme_list_size%NUM_COLUMNS))
],
]
def main():
global selected_theme
window_theme_browser = sg.Window('Theme Browser', theme_browser_layout, resizable=True, finalize=True, location=(100,200))
window = create_theme_example(selected_theme)
while True:
event, values = window_theme_browser.read()
if event == sg.WINDOW_CLOSED or event == 'Exit':
break
window.close()
selected_theme = theme_list[int(event)]
print("Selected:",selected_theme )
window = create_theme_example(selected_theme)
window_theme_browser.close()
window.close()
if __name__ == '__main__':
main()
If you don't want to close and create the window but just update it with a new theme, and don't mind if it is portable or not, you can do it using tkinter. An example can be found in this thread: https://github.com/PySimpleGUI/PySimpleGUI/issues/2437