Python PySimpleGUI Tutorial #02: Creating Buttons and Menu Elements

Posted by


In this tutorial, we will be focusing on how to create buttons and menu elements using Python PySimpleGUI library. PySimpleGUI is a powerful, easy-to-use GUI library for Python that allows you to quickly create GUI applications with minimal code.

To get started, make sure you have PySimpleGUI installed on your machine. You can install it using pip:

pip install PySimpleGUI

Now, let’s dive into creating buttons and menu elements in PySimpleGUI.

Creating Buttons:
Buttons are one of the most common GUI elements used in applications. In PySimpleGUI, you can create buttons using the Button element. Here’s an example of how to create a basic window with a button:

import PySimpleGUI as sg

layout = [[sg.Button('Click me')]]

window = sg.Window('Button Example', layout)

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

    if event == sg.WIN_CLOSED:
        break

window.close()

In this code snippet, we create a simple window with a single button labeled "Click me". The sg.Window function is used to create a window with the specified layout, and the window.read() function is used to read events from the window. When the button is clicked, the event variable will be set to the button’s text.

Creating Menu Elements:
Menu elements are used to create dropdown menus in GUI applications. PySimpleGUI provides the Menu and Menu Element elements for creating menu elements. Here’s an example of how to create a window with a simple dropdown menu:

import PySimpleGUI as sg

layout = [
    [sg.Menu([
        ['File', ['Open', 'Save', 'Exit']],
        ['Edit', ['Cut', 'Copy', 'Paste']]
    ])]
]

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 create a window with a menu containing two main menu items: "File" and "Edit". Each main menu item has a dropdown menu associated with it, containing the specified submenu items.

Overall, PySimpleGUI provides a simple and intuitive way to create buttons and menu elements in GUI applications. By following the examples provided in this tutorial, you should be able to easily create buttons and menu elements for your own PySimpleGUI applications.

0 0 votes
Article Rating

Leave a Reply

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@joedulis
14 days ago

Awesome! So much functions and very flexible! Thanks!

@MAJJASTAK
14 days ago

Sorry for being very late, but i seem to have a weird difference, being that if i have no value to a button, the program crashes instead of just doing nothing when you click said button, like in your video.

but the value of all buttons do indeed appear in the variable explorer so i find it weird that is crashes, also find it weird that when values is = {0:'audio'} (when i click the 'audio' button, like in the cideo again) – which means i did read the value of my click – it still results in a crash

same crash happens in the next tutorial when i close the popup after entering a name and confirming with ok, the popup does and appear and does print the name, but it just crashes when it comes back to the main window so it might be the exact same reason as the previous one

Might be a problem with Spyder and its kernel though… I have trouble knowing this kind of stuff, being new to programming (atleast for an app, i actually got 2 years of python and a year of C from school) and such.

@goofyAhhTrashBin
14 days ago

great video, but i have a question, is there any way i can make a button open another window? if its possible, can you please tell me how.

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