”;
A function in PySimpleGUI module that start with the prefix popup* generates window of a predefined appearance. The name of the popup function indicates is purpose and configuration of buttons present on it. These popups are created with just one line of code. Each popup serves a certain purpose, and then closes immediately.
A most basic popup is created by the popup()function. It can be used like a print() function to display more than one parameters on the window, and an OK button. It acts like a message box, that disappears immediately on pressing the OK button
>>> import PySimpleGUI as psg >>> psg.popup("Hello World")
It displays a popup window with Hello World text and OK button. Note that more than one strings can be displayed. Following popups with different button configurations are available −
-
popup_ok − Display Popup with OK button only
-
popup_ok_cancel − Display popup with OK and Cancel buttons
-
popup_cancel − Display Popup with “cancelled” button text
-
popup_yes_no − Display Popup with Yes and No buttons
-
popup_error − Popup with colored button and ”Error” as button text
These functions return the text of the button pressed by the user. For example, if the user presses OK button of the ok-cancel popup, it returns Ok which can be used in further programming logic.
Following popups accept input from the user in the form of text or let the user select file/folder/date from the selectors.
-
popup_get_text − Display Popup with text entry field. Returns the text entered or None if closed / cancelled
-
popup_get_file − Display popup window with text entry field and browse button so that a file can be chosen by user.
-
popup_get_folder − Display popup with text entry field and browse button so that a folder can be chosen.
-
popup_get_date − Display a calendar window, get the user”s choice, return as a tuple (mon, day, year)
When user has made the selection and Ok button is pressed, the return value of the popup is the text, which can be used further in the program.
Following script shows the use of some of the above popups −
import PySimpleGUI as psg text = psg.popup_get_text(''Enter your name'', title="Textbox") print ("You entered: ", text) file=psg.popup_get_file(''Select a file'', title="File selector") print ("File selected", file) folder=psg.popup_get_folder(''Get folder'', title="Folder selector") print ("Folder selected",folder) ch = psg.popup_yes_no("Do you want to Continue?", title="YesNo") print ("You clicked", ch) ch = psg.popup_ok_cancel("Press Ok to proceed", "Press cancel to stop", title="OkCancel") if ch=="OK": print ("You pressed OK") if ch=="Cancel": print ("You pressed Cancel") psg.popup_no_buttons(''You pressed'', ch, non_blocking=True) psg.popup_auto_close(''This window will Autoclose'')
Output − The popups generated by the above code are shown below −
The following output is displayed on the Python console −
You entered: Tutorialspoint File selected F:/python36/hello.png Folder selected F:/python36/Scripts You clicked Yes You pressed Cancel
All types of popups are objects of respective classes inherited from popup class. All of them have a common set of properties. These properties have a certain default value, and can be used to customize the appearance and behaviour of the popup objects. Following table lists the common parameters −
Type | Parameter | Description |
---|---|---|
Any | *args | Values to be displayed on the popup |
Str | title | Optional title for the window. |
(str, str) or None | button_color | Color of the buttons shown (text color, button color) |
Str | background_color | Window”s background color |
Str | text_color | text color |
Bool | auto_close | If True the window will automatically close |
Int | auto_close_duration | time in seconds to keep window open before closing it automatically |
Bool | non_blocking | If True then will immediately return from the function without waiting for the user”s input. |
Tuple[font_name, size, modifiers] | font | specifies the font family, size, etc. Tuple or Single string format ”name size styles”. |
Bool | grab_anywhere | If True can grab anywhere to move the window. |
(int, int) | Location | Location on screen to display the top left corner of window. Defaults to window centered on screen |
Bool | keep_on_top | If True the window will remain above all current windows |
Bool | modal | If True, then makes the popup will behave like a Modal window. Default = True |
Scrolled Popup
The popup_scrolled() function generates a popup with a scrollable text box in it. Use this to display a large amount of text, consisting of many lines with number of characters more than the width.
The size property is a tuple (w, h) with “w” being the number of characters in one line, and “h” being the lines displayed at a time. The horizontal/vertical scrollbar to the text box will become active if the number of characters/no of lines of text are more than “w” or “h”.
In the following example, a big file zen.txt is displayed in a popup with scrollable text box. The file contains the design principles of Python called the “Zen of Python”.
import PySimpleGUI as psg file=open("zen.txt") text=file.read() psg.popup_scrolled(text, title="Scrolled Popup", font=("Arial Bold", 16), size=(50,10))
It will produce the following output −
Progress Meter
The “one_line_progress_meter” is a popup that displays the visual representation of an ongoing long process, such as a loop. It shows the instantaneous value of a certain parameter, estimated time to complete the process, and the elapsed time.
In the following example, a text file is read character by character. The Progress meter shows the progress of the process in the form of a progress bar, estimated time required for completion, and the instantaneous value of the count.
import PySimpleGUI as psg import os size = os.path.getsize(''zen.txt'') file=open("zen.txt") i=0 while True: text=file.read(1) i=i+1 if text=="": file.close() break print (text,end='''') psg.one_line_progress_meter( ''Progress Meter'', i, size, ''Character Counter'' )
It will produce the following output window −
Debug Popup
During the execution of a program, it is usually required to keep track of intermediate values of certain variables, although not required in the following output. This can be achieved by the Print() function in PySimpleGUI library.
Note − Unlike Python’s built-in print() function, this function has “P” in uppercase).
As the program encounters this function for the first time, the debug window appears and all the subsequent Prints are echoed in it. Moreover, we can use EasyPrint or eprint that also have same effect.
The following program computes the factorial value of the number input by the user. Inside the for loop, we want to keep track of the values of f (for factorial) on each iteration. That is done by the Print function and displayed in the debug window.
import PySimpleGUI as psg f=1 num=int(psg.popup_get_text("enter a number: ")) for x in range(1, num+1): f=f*x psg.Print (f,x) print ("factorial of {} = {}".format(x,f))
Assuming that the user inputs 5, the debug window shows the following output−
”;