15.2 PySimpleGUI Standard Dialog Boxes

There are many common programming tasks that can be performed using pre-defined GUI dialog boxes. The following discussion describes these dialog boxes and provides some simple examples. You can refer to the PySimpleGUI documentation for additional optional parameters.

15.2.1 Messages

A messagebox can display information to a user. There are many variations on these dialog boxes based on the type of message you want to display. The functions use keyword arguments to configure their look and behavior. The functions return a string which is typically ignored.


import PySimpleGUI as sg

sg.popup('Information message', title='Information')
sg.popup_error('Error message', title='Error')
      

15.2.2 Yes/No Questions

The PySimpleGUI popup functions also allow you to ask a user simple yes/no type questions and varies the button names based on the type of question. These functions are:


import PySimpleGUI as sg

sg.popup_ok_cancel('Do you want to try that again?', title='Question')
sg.popup_yes_no('Do you like Python?', title='Question')
      

The return value is the name of the button clicked.

15.2.3 Single Value Data Entry

If you want to ask the user for a single data value, either a string, integer, or floating point value, you can use a popup_get_text function. A user can enter the requested value and hit “OK”, which will return the entered value. If the user hits “Cancel,” then None is returned.


import PySimpleGUI as sg

answer = sg.popup_get_text('What is your first name?', title='Input')

if answer is not None:
    print("Your first name is ", answer)
else:
    print("You don't have a first name?")

answer = sg.popup_get_text('What is your age?', title='Input')

try:
    answer = int(answer)
    print("Your age is ", answer)
except:
    print("You don't have an age?")

answer = sg.popup_get_text('What is your salary?', title='Input')

try:
    answer = float(answer)
    print("Your salary is ", answer)
except:
    print("You don't have a salary?")
      

15.2.4 File Chooser

A common task is to select the names of folders and files on a storage device. This can be accomplished using a filedialog object. Note that these commands do not save or load a file. They simply allow a user to select a file. Once you have the file name, you can open, process, and close the file using appropriate Python code. These dialog boxes always return you a “fully qualified file name” that includes a full path to the file. Also note that if a user is allowed to select multiple files, the return value is a tuple that contains all of the selected files. If a user cancels the dialog box, the returned value is an empty string.


import PySimpleGUI as sg
import os

# Build a list of tuples for each file type the file dialog should display
my_filetypes = (('all files', '.*'), ('text files', '.txt'))

# Ask the user to select a single file name.
answer = sg.popup_get_file('Please select a file:', default_path=os.getcwd(), file_types=my_filetypes)
print(f'File selected: {answer}')

# Ask the user to select a single file name for saving.
layout = [[sg.Text('Please select a file name for saving:'),
           sg.FileSaveAs(initial_folder=os.getcwd(), file_types=my_filetypes, key='-SAVE-'),
           sg.Button('OK'), sg.Button('Cancel')]]

window = sg.Window('Save as...', layout)

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

    if event == sg.WIN_CLOSED or event == 'Cancel':
        break
    elif event == 'OK':
        if values['-SAVE-']:
            # code to write to file goes here
            print(f'code to write to \"{values["-SAVE-"]}\" file goes here')
        break

window.close()
      

15.2.5 Color Chooser

PySimpleGUI includes a nice dialog box for choosing colors. You provide it with an initial color (in tuple, hex, or color name formats), and it returns a color in two different specifications: 1) an RGB value as a tuple, such as (255, 0, 0) which represents red, and 2) a hexadecimal string used in web pages, such as "#FF0000" which also represents red. If the user cancels the operation, the return values are None and None.


import PySimpleGUI as sg

rgb_color, web_color = sg.askcolor((255, 0, 0))