2020 PySimpleGUI Part 5 – Keyboard Shortcuts

Posted by


In this tutorial, we will be discussing shortcuts in PySimpleGUI. Shortcuts are keyboard inputs that trigger a specific action within your PySimpleGUI application. This can be used to enhance the user experience by allowing them to navigate the application more efficiently.

To create shortcuts in PySimpleGUI, we will use the return_keyboard_events=True parameter when creating the layout for our window. This will allow us to capture keyboard events and map them to specific actions within our application.

Let’s start by creating a simple PySimpleGUI application with a shortcut to close the window when the user presses the ‘Ctrl + Q’ keys.

import PySimpleGUI as sg

layout = [
    [sg.Text('Press Ctrl + Q to close the window')],
]

window = sg.Window('Shortcut Example', layout, return_keyboard_events=True)

while True:
    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Escape:27'):
        break
    elif event == 'x11q':  # Ctrl + Q
        window.close()

window.close()

In the example above, we have created a window with a single text element instructing the user to press ‘Ctrl + Q’ to close the window. We have set return_keyboard_events=True when creating the window so that we can capture keyboard events.

In the event loop, we check for the keyboard event that corresponds to ‘Ctrl + Q’ (which is represented as ‘x11q’ in PySimpleGUI). When this event is detected, we close the window using the window.close() method.

You can define custom shortcuts for your PySimpleGUI application by mapping keyboard events to specific actions within your code. You can refer to the ASCII values of different keys to define your shortcuts. Additionally, you can use modifiers like ‘Ctrl’, ‘Alt’, and ‘Shift’ to create more complex shortcuts.

import PySimpleGUI as sg

# Define the layout
layout = [
    [sg.Text('Press Ctrl + A to display a message')],
]

# Create the window with keyboard events enabled
window = sg.Window('Custom Shortcuts', layout, return_keyboard_events=True)

# Event loop to capture keyboard events
while True:
    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Escape:27'):
        break
    elif event == 'x01a':  # Ctrl + A
        sg.popup('Hello, this is a custom shortcut!')

window.close()

In this example, we have created a window with a custom shortcut ‘Ctrl + A’ that displays a popup message when triggered. We have used the ASCII value ‘x01’ for ‘Ctrl’ and ‘x61’ for ‘A’ to define the shortcut.

You can explore different keyboard events and modifiers to create shortcuts that enhance the usability of your PySimpleGUI application. Shortcuts can greatly improve the user experience by allowing users to quickly navigate through the application using keyboard inputs. Experiment with different shortcuts and actions to create a more intuitive interface for your users.

0 0 votes
Article Rating

Leave a Reply

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@sbunyamin
2 hours ago

thanks for everything this is so useful and great

@mrelbe
2 hours ago

The fact that docstrings are not presented by PyCharm for a new identifier assigned to a function is not a bug in PyCharm. That’s how Python works; the docstring is not copied along with the assignment.

2
0
Would love your thoughts, please comment.x
()
x