In this lesson we want to learn How to Build Python GUI with PySimpleGUI, PySimpleGUI is Python package that makes it easy to create graphical user interfaces (GUIs) for your Python programs. this is an example of how to create simple GUI using PySimpleGUI:
- First, install the PySimpleGUI package using pip:
1 |
pip install pysimplegui |
- Next, import the package:
1 |
import PySimpleGUI as sg |
- Now, create layout for your GUI. You can use the sg.Layout method to create layout using the element types provided by the package, such as buttons, text boxes and labels. for example:
1 2 3 4 5 |
layout = [ [sg.Text('Enter your name:')], [sg.InputText()], [sg.Button('OK'), sg.Button('Cancel')] ] |
- Create the window by calling sg.Window method and passing in the layout title for the window and any other optional parameters you want to specify. for example:
1 |
window = sg.Window('My GUI', layout) |
- Use read method to handle user input and events in the window. for example following code will display the window and wait for the user to click the OK or Cancel button:
1 |
event, values = window.read() |
- Use an if statement to check the value of event variable, and then perform the appropriate action based on the button that was clicked. for example:
1 2 3 4 |
if event == 'OK': print('Hello, ' + values[0]) else: print('Goodbye') |
- Close the window with close() method
1 |
window.close() |
This is just basic example of how to create GUI using PySimpleGUI. you can also add more elements to your layout, create custom styles for your elements and handle more complex events and user input.
You can find more information about PySimpleGUI on the package’s website, which includes documentation and a lot of examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import PySimpleGUI as sg # Define layout of GUI layout = [ # This is our tex element [sg.Text('Enter your name:')], # This is Input fild for user [sg.InputText()], # These are Buttons for user interaction [sg.Button('OK'), sg.Button('Cancel')] ] # Create window with layout window = sg.Window('My GUI', layout) # Read events and values from window event, values = window.read() # Check which button was clicked if event == 'OK': print('Hello, ' + values[0]) else: print('Goodbye') # Close the window window.close() |
Run the complete code and this will be the result
FAQs:
How do I create a GUI in PySimpleGUI?
Creating a GUI in PySimpleGUI is straightforward. You start by defining the layout of your GUI using a list of lists, where each inner list represents a row of elements. After that you create a sg.Window object with the defined layout. After that, you read events and values from the window using window.read() and handle user interactions accordingly. Finally, don’t forget to close the window when done.
How to build GUI using Python?
For building a GUI using Python, you have several options, including Tkinter, PyQt, wxPython and PySimpleGUI. With PySimpleGUI, you can quickly create GUIs with a simple and easyAPI. First, you need to install PySimpleGUI using pip (pip install PySimpleGUI). after that, follow the documentation and examples provided to define your GUI layout and handle user interactions.
What can you do with PySimpleGUI?
PySimpleGUI is a Python GUI framework that allows you to create desktop applications in easy way. With PySimpleGUI, you can build different types of GUI applications, including simple utilities, data visualization tools, image processing applications, games and more. It provides different GUI elements and functionalities to suit different project requirements.
Subscribe and Get Free Video Courses & Articles in your Email