Pysimplegui: Here is a way to load custom themes from outside of PySimpleGui.py

Created on 6 Oct 2018  路  14Comments  路  Source: PySimpleGUI/PySimpleGUI

I thought for a few minutes and then just went for it. Needed a way to distribute without everyone changing the PSG source for themes. This works pretty well. Perhaps adding this to docs would help someone else that wants to define themes without modifying source:

def ChangeLookAndFeel(index):
    look_and_feel =  {'Topanga_Dark': {'BACKGROUND': '#282923', 'TEXT': '#E7DB74', 'INPUT': '#282923',
                      'SCROLL': '#E7C855', 'TEXT_INPUT': '#45473D', 'BUTTON': ('#E7C855', '#284B5A'),
                      'PROGRESS': "#282923", 'SCROLL': '#282923', 'BORDER': 1,'SLIDER_DEPTH':0, 'PROGRESS_DEPTH':0}}
try:
    colors = look_and_feel[index]

    sg.SetOptions(background_color=colors['BACKGROUND'],
                  text_element_background_color=colors['BACKGROUND'],
                  element_background_color=colors['BACKGROUND'],
                  text_color=colors['TEXT'],
                  input_elements_background_color=colors['INPUT'],
                  button_color=colors['BUTTON'],
                  progress_meter_color=colors['PROGRESS'],
                  border_width=colors['BORDER'],
                  slider_border_width=colors['SLIDER_DEPTH'],
                  progress_meter_border_depth=colors['PROGRESS_DEPTH'],
                  scrollbar_color=(colors['SCROLL']),
                  element_text_color=colors['TEXT'],
                  input_text_color=colors['TEXT_INPUT'])
except:    # most likely an index out of range
    pass

ChangeLookAndFeel('Topanga_Dark')

Most helpful comment

Get the latest from Master Branch and then this will work:

sg.LOOK_AND_FEEL_TABLE['Topanga_Dark'] = {'BACKGROUND': '#282923', 'TEXT': '#E7DB74', 'INPUT': '#282923',
                      'SCROLL': '#E7C855', 'TEXT_INPUT': '#45473D', 'BUTTON': ('#E7C855', '#284B5A'),
                      'PROGRESS': "#282923", 'SCROLL': '#282923', 'BORDER': 1,'SLIDER_DEPTH':0, 'PROGRESS_DEPTH':0}

sg.ChangeLookAndFeel('Topanga_Dark')

All 14 comments

Have you tried sg.ChangeLookAndFeel('Topanga') ?

Of course :) But that requires the theme defined in PySimpleGUI.py, if you define it like this in your own module, you can define the theme without modifying your source. You see, I just changed SetOptions, to sg.SetOptions and made ChangeLookAndFeel a definition within my own module.

I've got no problem further exposing the table so that it can be added to using an import PySimpleGUIThemes statement

Then you guys can go nuts making your own themes and saving them to a Themes file.

That would be cool. This workaround will do fine until then :D
Check out the top of https://github.com/eagleEggs/topanga/blob/master/scripts/topanga.py
to see what I'm talking about. I don't define the theme within PySimpleGUI.py anymore.

Ah, OK , I understand a bit better now.. you're redefining the entire function name. All other calls will fail using this method or can I load the old themes after your import?

I was thinking I would make the table a global that can then be added to.

sg.ThemeTable['TopangaDark'] = ....

Does your version lose the ability to call SetLookAndFeel('GreenTan') ?

Any calls after should by default be sg.ChangeLookAndFeel so i don't think there is a conflict...

Yes the call works because my function is in my module. Calling sg.ChangeLookAndFeel accesses yours and overwrites anything mine did.

So when this happens, it goes GreenTan:

ChangeLookAndFeel('Topanga_Dark') # my function
sg.ChangeLookAndFeel('GreenTan') # your function

The main importance is calling sg.SetOptions - Which calls your sg.SetOptions and passes the table I have defined in my module.

What I meant was a unified way... modify your function so that your code calls only 1 function.

Not important.... I'm almost done with the code change.

Nice! I didn't want to bother you with more color stuff but man you're quick. I see what you mean though.

Thinking about a way to non intrusively let users choose a theme. Maybe take in args and bake it into the windows shortcut... I don't want more pop up windows to go through to select something like that before the main window draws.

Get the latest from Master Branch and then this will work:

sg.LOOK_AND_FEEL_TABLE['Topanga_Dark'] = {'BACKGROUND': '#282923', 'TEXT': '#E7DB74', 'INPUT': '#282923',
                      'SCROLL': '#E7C855', 'TEXT_INPUT': '#45473D', 'BUTTON': ('#E7C855', '#284B5A'),
                      'PROGRESS': "#282923", 'SCROLL': '#282923', 'BORDER': 1,'SLIDER_DEPTH':0, 'PROGRESS_DEPTH':0}

sg.ChangeLookAndFeel('Topanga_Dark')

Sweet! Nice work

All you have to do is ask :-)

Made an announcement of the new PSG feature that enables users to create their own and share it... so closing this one.

Keep the ideas flowing! I'll keep taking the best of 'em and putting them into the code.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DKatarakis picture DKatarakis  路  6Comments

mozesa picture mozesa  路  4Comments

yogesh-aggarwal picture yogesh-aggarwal  路  3Comments

OndoyManing picture OndoyManing  路  4Comments

flowerbug picture flowerbug  路  4Comments