Creating a Graphical User Interface (GUI) App with PySimpleGUI

Creating a Graphical User Interface (GUI) App with PySimpleGUI

Python tutorial on how to write a graphical user interface (GUI) app with PySimpleGUI and how to create a standalone executable file from it:

Creating a Graphical User Interface (GUI) App with PySimpleGUI

PySimpleGUI is a powerful and easy-to-use Python library for creating graphical user interfaces. It is cross-platform, so your apps will work on Windows, macOS, and Linux. In this tutorial, we will show you how to create a simple GUI app with PySimpleGUI.

1. Installing PySimpleGUI

The first step is to install the PySimpleGUI library. You can do this using pip:

pip install pysimplegui        

2. Creating a Basic GUI App

To create a basic GUI app with PySimpleGUI, you will need to import the library and create a window object. You can then add widgets to the window, such as buttons, labels, and inputs.

Python

import PySimpleGUI as sg

# Create a window
window = sg.Window('My App', layout=[
    [sg.Text('Enter your name:'), sg.InputText()],
    [sg.Button('Submit')],
    [sg.Output(size=(20, 10))]
])

# Show the window and get the user's input
event, values = window.Read()

# Print the user's name
if event == 'Submit':
    print(values['InputText'])

# Close the window
window.Close()
        

Use code with caution. Learn more

content_copy

Creating a Standalone Executable File

To create a standalone executable file from your PySimpleGUI app, you can use the PySimpleGUI-exemaker library. This library will compile your Python code into a native executable file that can be run without Python installed.

pip install pysimplegui-exemaker
        

To create an executable file for your app, you will need to run the following command:

python -m pysimplegui-exemaker.pysimplegui_exemaker my_app.py
        

This will create an executable file called my_app.exe. You can then run this file to run your app.

Here is an example of how to use the PySimpleGUI-exemaker library to create an executable file for the app we created earlier:

Python

import PySimpleGUI as sg
import pysimplegui_exemaker as pxe

# Create a window
window = sg.Window('My App', layout=[
    [sg.Text('Enter your name:'), sg.InputText()],
    [sg.Button('Submit')],
    [sg.Output(size=(20, 10))]
])

# Show the window and get the user's input
event, values = window.Read()

# Print the user's name
if event == 'Submit':
    print(values['InputText'])

# Close the window
window.Close()

# Create an executable file
pxe.create_exe(my_app.py)
        

Use code with caution. Learn more


Sources:

要查看或添加评论,请登录

Ehsan Tork的更多文章

社区洞察

其他会员也浏览了