Search code examples
pythonpython-3.xmenupysimplegui

How to add image/icon in menu item on pysimplegui


I am using PySimplegui for UI development,want to know if there is a way to add menu icon as shown in the image below this can be done with Tkinter and is working fine ,but i am failed to find any references in pysimplegui any reference or link would be a great help

enter image description here expecting this in menu item (image side by text)


Solution

  • It's complex to add icons to menu items.

    Here, add icons to menu items one by one by tkinter code.

    import PySimpleGUI as sg
    
    sg.theme('LightGreen')
    
    menu_def = [
        ['&File', ['&Open     Ctrl-O', '&Save       Ctrl-S', '&Properties', 'E&xit']],
        ['&Edit', ['&Paste', ['Special', 'Normal', ], 'Undo', 'Options::this_is_a_menu_key'], ],
        ['&Toolbar', ['---', 'Command &1', 'Command &2',
                      '---', 'Command &3', 'Command &4']],
        ['&Help', ['&About...']]
    ]
    
    
    layout = [
        [sg.Menu(menu_def, key='-MENUBAR-')],
        [sg.Output(size=(60, 10))],
    ]
    
    window = sg.Window("Title", layout, finalize=True)
    
    images = []
    
    # Menu 1 - File
    for i in range(4):
        image = sg.tk.PhotoImage(data=sg.EMOJI_BASE64_HAPPY_LIST[0:4][i])
        images.append(image)
        window['-MENUBAR-'].widget.children['!menu'].entryconfigure(i, image=image, compound='left')
    
    # Menu 2 - Edit
    for i in range(3):
        image = sg.tk.PhotoImage(data=sg.EMOJI_BASE64_HAPPY_LIST[4:7][i])
        images.append(image)
        window['-MENUBAR-'].widget.children['!menu2'].entryconfigure(i, image=image, compound='left')
    
    # Menu 2 - Edit - Paste
    for i in range(2):
        image = sg.tk.PhotoImage(data=sg.EMOJI_BASE64_HAPPY_LIST[7:9][i])
        images.append(image)
        window['-MENUBAR-'].widget.children['!menu2'].children['!menu'].entryconfigure(i, image=image, compound='left')
    
    # Menu 3 - Toolbar
    for i, j in enumerate((1, 2, 4, 5)):
        image = sg.tk.PhotoImage(data=sg.EMOJI_BASE64_HAPPY_LIST[9:13][i])
        images.append(image)
        window['-MENUBAR-'].widget.children['!menu3'].entryconfigure(j, image=image, compound='left')
    
    # Menu 4 - Help
    for i in range(1):
        image = sg.tk.PhotoImage(data=sg.EMOJI_BASE64_HAPPY_LIST[13])
        images.append(image)
        window['-MENUBAR-'].widget.children['!menu4'].entryconfigure(i, image=image, compound='left')
    
    while True:
        event, values = window.read()
        if event in (sg.WIN_CLOSED, 'Exit'):
            break
        print(event, values)
    
    window.close()
    

    enter image description here