PySimpleGUI Menu #6

Posted by


In this tutorial, we will learn how to create a menu in PySimpleGUI using Python. A menu is a common graphical user interface component that allows users to access different functionalities of an application. PySimpleGUI provides an easy way to create menus in your GUI applications.

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

pip install PySimpleGUI

Now, let’s create a simple GUI application with a menu. We will create a menu with two options – "File" and "Help".

import PySimpleGUI as sg

# Define the layout of the window
layout = [
    [sg.Menu([
        ['&File', ['&Open', '&Save', '---', '&Exit']],
        ['&Help', ['&About']]
    ])],
    [sg.Text('Click on the menu to see the options')]
]

# Create the window
window = sg.Window('Menu Example', layout)

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

    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    elif event == 'About':
        sg.popup('This is a simple PySimpleGUI menu example')
    elif event == 'Open':
        sg.popup('Opening a file')
    elif event == 'Save':
        sg.popup('Saving the file')

window.close()

In this code, we first import the PySimpleGUI library. We define the layout of the window with a sg.Menu element containing two menu options – "File" and "Help". Each menu option has sub-items that show up when you click on them.

We create a window with the layout and enter an event loop where we handle different menu events. If the user clicks on "Exit", the program exits. If the user clicks on "About", a popup with a message is displayed. If the user clicks on "Open", a popup saying "Opening a file" is displayed. If the user clicks on "Save", a popup saying "Saving the file" is displayed.

You can customize the menu by adding more options, sub-items, and event handling logic. Play around with different menu options to create a more advanced GUI application.

I hope this tutorial was helpful in understanding how to create a menu using PySimpleGUI. Try experimenting with the code and explore more features provided by PySimpleGUI to create interactive GUI applications. Happy coding!

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@adrtalladi2404
2 hours ago

писец как мелко – бляяя , смотреть невозможно

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