
Hi !
After my base sample code for a GUI app, now it’s time to add some interaction features:
- Context Menu
- Capture Key Press
In the following example, I’m adding a context menu with the following elements:

This can be done as part of the window definition, in example
right_click_menu = ['Unused', ['&FPS', '---', 'Menu A', 'Menu B', 'Menu C', ['Menu C1', 'Menu C2'], '---', 'Exit']]
window = sg.Window("El Bruno - Webcams and GrayScale with PySimpleGUI", layout,
right_click_menu=right_click_menu,
no_titlebar=False, alpha_channel=1, grab_anywhere=False,
return_keyboard_events=True, location=(100, 100))
The menu definition and how to create submenus, separators, quick access keys and more are part of the PySimpleGUI documentation.
And then, in order to capture events in the window, we need to check the events read in each loop of the while. The following sample, check the window events
- Close the Window is the user press the [X] to close the window, or click the [Exit] element on the context menu
- Change the value of a boolean var if the user press the key [F] , or click the [FPS] element on the context menu
# process windows events
event, values = window.read(timeout=20)
if event == sg.WIN_CLOSED or event == "Exit":
break
if event == "f" or event == "F" or event == "FPS":
display_fps = not display_fps
The full code:
Happy coding!
Greetings
El Bruno
More posts in my blog ElBruno.com.
More info in https://beacons.ai/elbruno
Resources
Face Recognition and Face Detection series in Python
- Detecting Faces with 20 lines in Python
- Face Recognition with 20 lines in Python
- Detecting Facial Features with 20 lines in Python
- Facial Features and Face Recognition with 20 lines in Python
- Performance improvements with code
- Resize the camera input with OpenCV
- Working with Haar Cascades and OpenCV
- Detect and blur faces 😁 using haar cascades
- Detect and blur faces 😁 using DNN