In this tutorial, we will be exploring the latest version of PySimpleGUI 2020 Part 12, focusing on menus. This version is considered experimental and faster, offering improved performance and efficiency. We will cover three types of menus in PySimpleGUI: Menubar, Right Click Menu, and ButtonMenus.
- Menubar:
A Menubar is a horizontal bar at the top of a window or frame containing menus. It allows users to access various functions and options easily. To create a Menubar in PySimpleGUI, use the menu function with the desired menu items as parameters. Here’s an example:
import PySimpleGUI as sg
menu_layout = [['File', ['Open', 'Save', 'Exit']], ['Edit', ['Cut', 'Copy', 'Paste']]]
layout = [
[sg.Menu(menu_layout)],
[sg.Text('Hello World!')]
]
window = sg.Window('Menu Example', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
window.close()
In this code snippet, we define a Menubar with two menus – File and Edit. Each menu has its own submenu items. When you run the code, you will see a Menubar at the top of the window with the specified menu items.
- Right Click Menu:
A Right Click Menu is a context menu that appears when the user right-clicks on a specific element, such as a button or text. It provides quick access to relevant options or actions. To create a Right Click Menu in PySimpleGUI, use the right_click_menu function with the desired menu items as parameters. Here’s an example:
import PySimpleGUI as sg
layout = [
[sg.Text('Right-click me!', enable_events=True, right_click_menu=['Context', ['Option 1', 'Option 2']])]
]
window = sg.Window('Right Click Menu Example', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'Option 1':
sg.popup('Option 1 selected!')
elif event == 'Option 2':
sg.popup('Option 2 selected!')
window.close()
In this code snippet, we create a Text element that triggers a Right Click Menu when right-clicked. The menu contains two options – Option 1 and Option 2. Handling the event for each option allows you to perform actions based on the user’s selection.
- ButtonMenus:
ButtonMenus are menus that appear when a button is clicked. They provide additional options or functionalities related to the button. To create a ButtonMenu in PySimpleGUI, use the button_menu function with the desired menu items as parameters. Here’s an example:
import PySimpleGUI as sg
layout = [
[sg.Button('Open', button_menu=['File', ['New', 'Open', 'Save']])]
]
window = sg.Window('ButtonMenu Example', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'New':
sg.popup('New option selected!')
elif event == 'Open':
sg.popup('Open option selected!')
elif event == 'Save':
sg.popup('Save option selected!')
window.close()
In this code snippet, we create a Button with a ButtonMenu that appears when the button is clicked. The menu contains three options – New, Open, and Save. Handling the event for each option allows you to perform actions based on the user’s selection.
Overall, menus play a crucial role in providing users with access to various features and functionalities in an application. PySimpleGUI’s experimental faster version offers enhanced performance and efficiency, making it easier to create interactive and user-friendly interfaces with menus. Try implementing Menubars, Right Click Menus, and ButtonMenus in your PySimpleGUI projects to enhance the user experience.
Thank you so much for the practical tutorials!
I have one question though about PysimpleGui if you don't mind I ask here.
Do you know how to open(read only) selected multiple items (files with different formats) at once in Listbox output using PysimpleGui..also to SAVE them at once when an event (either Read or Save) is triggered by clicking right_click_menu ?
I was able to code select multiple items in the Listbox after searching files with specific term and do right click to choose read or save. But struggling to operate the those right click menu: especially open(read only) one to many selected files or save them at once in selected location by user or default path like (user's download folder),,
Can you please help?
😊simply perfect