2

I want to add a feature to change the background using the menu but it doesn't work. When I click on any menu item, nothing happens, the background doesn't change and it doesn't print anything. Maybe I'm also doing the background change in a wrong way, so, if someone could help me, I would be happy.

This is my code:

from time import sleep
import os
try:
    import PySimpleGUI as sg
except ModuleNotFoundError:
    os.system('pip3 install PySimpleGUI')
import sys

theme = ('dark grey 9')

sg.theme(theme)

menu_def = [['Customize GUI', ['Background', ['White::white', 'Purple::purple']]]]

layout = [[sg.Menu(menu_def)]]

window = sg.Window('Fast reader by Hary', layout)

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    if event == 'Purple::purple':
        theme = 'LightPurple'
        window.refresh()
    elif event == 'White::white':
        theme = 'DarkGrey6'
        window.refresh()
Frightera
  • 4,773
  • 2
  • 13
  • 28
zHary_
  • 75
  • 6
  • 1
    "just typing some random things..."—please don't do this. "the site says me to add some more details"—so maybe _add some more details?_ The rules are here to help _you_ as much as us. Asking a good question greatly increases the chances that you'll get a helpful response. Please read [ask], paying particular attention to the [mcve] part (emphasis on "minimal") and the part asking you to pretend you're talking to a busy colleague. Show some respect for our time. – ChrisGPT was on strike Apr 03 '21 at 22:34
  • k, sorry, I didn't know what to type but I'll edit it now. – zHary_ Apr 03 '21 at 22:40

2 Answers2

2

On this line:

if menu_def == 'purple':

The variable menu_def is a whole list, it cannot be equal to purple. I believe you meant to say if event == 'Purple'?

Anis R.
  • 6,656
  • 2
  • 15
  • 37
  • 1
    @zHary_ Kindly check edit. I'm not the most familiar with this library, but I guess you need to use `"Purple"` (uppercase), since your menu button is called like that. Otherwise, simply try `print(event)` right after the `window.read()`, this will tell you what the exact string you're looking for should be. – Anis R. Apr 03 '21 at 23:03
  • I did it. The event is "Purple::purple", it worked, it actually prints the "It worked!" but the background does not change. – zHary_ Apr 03 '21 at 23:44
  • Searching, I discovered that I probably can't change the background color of this window... If you know any way to do that, tell me but I think it's impossible. – zHary_ Apr 04 '21 at 04:03
2

Use 'Menu item::optional_key' as event in your event loop.

There're sub-menu under menu, so no easy setting for background color of all items in menu.

There're something wrong in your code

  • Method sg.theme(theme)only work before window finalized
  • Different value of user variable theme won't change the theme state.
  • Method window.refresh() is not required if next statement will be window.read() which will update window.

For new theme and new setting for Menu, new window will be preferred. Demo code as following,

import PySimpleGUI as sg

def main_window(theme, background_color, window=None):

    sg.theme(theme)

    menu_def = [['Customize GUI', ['Background', ['White::white', 'Purple::purple']]]]
    layout = [[sg.Menu(menu_def, key='-MENU-', text_color='black', background_color=background_color)]]
    new_window = sg.Window('Fast reader by Hary', layout, finalize=True)
    if window is not None:
        window.close()
    return new_window

theme, background_color = 'DarkGrey9', 'green'
window = main_window(theme, background_color)

while True:

    event, values = window.read()

    if event == sg.WINDOW_CLOSED:
        break
    elif event == 'Purple::purple':
        theme, background_color = 'LightPurple', 'purple'
        window = main_window(theme, background_color, window)
    elif event == 'White::white':
        theme, background_color = 'DarkGrey6', 'white'
        window = main_window(theme, background_color, window)

window.close()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23