Fausto Arinos Barbuto
Fausto Arinos Barbuto

Reputation: 398

Menu behaviour in PySimpleGUI and PySimpleGUIQt

I'm a novice PySimpleGUI user, but I'm liking it a great deal. The simple GUIs I've developed so far work well with both PySimpleGUI and PySimpleGUIQt. But today I stubbed my toe at something that's not seamlessly convertible. It's a very simple menu:

import PySimpleGUI as sg
...
    sg.theme('LightGreen5')
    menu_def = [['&File', ['&Open', 'C&lose'],]]

    layout = [[sg.Menu(menu_def, background_color=('white'), text_color=('black'), \
              font=('Arial'))], \ ...

This doesn't work with PySimpleGUIQt. It complains about the background_color and text colours, as well as about the font options. But if I remove those options, leaving only

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

it does work, but produces a quite cheesy, tasteless menu.

I much prefer Qt to tkinter (or whatever it is used when PySimpleGUIQt is not imported and used) but don't know what to do to use Qt and have the nice menu produced by tkinter (or whatever, etc). Do I have to create two distinct versions, one for Qt and another one to [whatever]? Any suggestions? Thanks.

Upvotes: 0

Views: 840

Answers (1)

Jason Yang
Jason Yang

Reputation: 13061

PySimpleGUIQt is just a User Alpha, so there's no options text_color and font in sg.Menu.

Here's workaround to provide color and font to sg.Menu.

import PySimpleGUIQt as sg

def menu_options(menu):

    style = """
        QMenuBar {
            background-color : white;
        }
        QMenuBar::item {
            spacing: 0px;
            padding: 2px 10px;
            background-color: white;
            color: black;
        }
        QMenuBar::item:selected {
            color: white;
            background-color: red;
        }
        QMenuBar::item:pressed {
            color: white;
            background: blue;
        }
        QMenu {
            color: black;
            background-color: white;
            border: 1px solid black;
            margin: 2px;
        }
        QMenu::item {
            background-color: transparent;
        }
        QMenu::item:selected {
            background-color: blue;
            color: white;
        }
    """

    menu = menu.Widget
    menu.setStyleSheet(style)

    font = sg.QFont('Courier New', 16, sg.QFont.Bold, True) # Family, size, bold, italic
    font.setUnderline(True)
    font.setStrikeOut(True)
    menu.setFont(font)
    for submenu in menu.findChildren(sg.QMenu):
        submenu.setFont(font)

sg.theme('LightGreen5')
menu_def = [
    ['&File', ['&Open', 'C&lose'],],
    ['&Edit', ['&Copy', '&Paste'],],
]
layout = [
    [sg.Menu(menu_def, key='-MENU-')],
    [sg.Text("Hello World", font=('Arial', 24))],
]
window = sg.Window('Title', layout, finalize=True)
menu_options(window['-MENU-'])

window.read()
window.close()

enter image description here

Upvotes: 4

Related Questions