Using PySimpleGUI: Creating Popups (Part 2)

Posted by


In this tutorial, we will learn about PySimpleGUI’s Popup feature, which allows us to display simple popup windows as a way to communicate with the user. The Popup feature is great for displaying alert messages, confirmation dialogs, input prompts, and more.

To get started, make sure you have PySimpleGUI installed. If you don’t already have it installed, you can do so by running the following command:

pip install PySimpleGUI

Now, let’s write some code to create a basic popup window using PySimpleGUI. Here’s a simple example that displays a message in a popup window:

import PySimpleGUI as sg

# Create a simple popup window
sg.Popup("Hello, world!")

When you run this code, you should see a popup window appear with the message "Hello, world!" displayed in it. This is a basic example of using the Popup feature in PySimpleGUI.

Next, let’s explore some additional options for creating different types of popup windows using PySimpleGUI. Here are some common popup window types:

  1. Alert message:
    An alert message is a simple popup window that displays a message to the user. You can create an alert message using the Popup function with the "title" keyword argument set to "Alert":
sg.Popup("This is an alert message", title="Alert")
  1. Confirmation dialog:
    A confirmation dialog is a popup window that asks the user to confirm an action. You can create a confirmation dialog using the Popup function with the "title" keyword argument set to "Confirmation" and the "buttons" keyword argument set to "OkCancel":
response = sg.Popup("Are you sure you want to delete this file?", title="Confirmation", buttons=("Yes", "No"))

if response == "Yes":
    # Delete the file
    pass
else:
    # Cancel the action
    pass
  1. Input prompt:
    An input prompt is a popup window that asks the user to enter some information. You can create an input prompt using the PopupGetText function, which returns the text entered by the user:
name = sg.PopupGetText("Please enter your name")
sg.Popup("Hello, {}".format(name))

These are just a few examples of the types of popup windows you can create using PySimpleGUI’s Popup feature. You can customize the appearance and behavior of popup windows by using different keyword arguments provided by the Popup and PopupGetText functions.

I hope this tutorial has given you a better understanding of how to use PySimpleGUI’s Popup feature to create popup windows in your Python applications. Have fun experimenting with different types of popup windows and customizing them to suit your needs!

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@dukescompany7855
1 month ago

Eu nao encontrei o poup para mostrar mensagem quando eu apertar o botao

@craigesloots9932
1 month ago

🤙