Creating Python GUI –Radio and Checkbox

A GUI application requires you to give option of selecting one or many values from a given set of options. For this the most commonly used Python GUI elements are Radio and Checkbox. Radio button is used when the user is allowed to select only one option from a set of options. If you want user to select gender (Male/Female/Transgender) or Residential area (Urban/ Rural/ Cosmopolitan) the element you will use is a Radio Button.

CheckBox is used when you want a user to select multiple options among given choices. If you want user to select his choice of Food or her academic qualifications, you can use Checkbox for Python GUI elements offered in pySimplegui package.

Radio and Checkbox

The radio button element provided by pySimplegui, to make a Python GUI application, has the following syntax with most commonly used attributes.

Radio( label_text,  radio_group_id, default,  disabled, size, background_color,
text_color,   font,  key,  tooltip,   visible)

The checkbox element provided by pySimplegui, to add in a Python GUI application, has the following syntax with most commonly used attributes.

Checkbox( label_text, default,  disabled, size, background_color, text_color,   font,  key, tooltip,   visible)

Attributes of Radio and Checkbox

Label_text– String, that appears with the radio button/checkbox to describe the purpose of a radio button.

radio_group_id – String,  name of the group of radio buttons among which only one option can be chosen. This attribute is not available for Checkbox element.

default– Boolean, used to define which radio button among one group  is/checkboxes are selected by default.

disabled– Boolean, used to enable or disable a radio button/checkbox so that user is restricted to use it. By default it is True means the element is available for interaction. To disable set enabled=False for both elements.

size-(int,int) as a pair of width and height of the radio button/checkbox.

Background-color– String, to set background color of the radio button/checkbox.

text-color–  String, to set the color of the label text of the radio button/checkbox.

font– String, use the valid font name to set the font of the label text.

Key/K– String, identity of the radio button/checkbox, by which it can be identified in the Python GUI window object using pySimpleGui

Tooltip– String, is the text that you want to display when a user rests mouse cursor on a specific element. It is used to display some information about the element.

Visible-Boolean, by default this is set to True, is used to control visibility of an element in a Python GUI application.

 To set any attribute of these two elements you must use the “attribute_name= value” pairs separated by comma.

Example to Demonstrate use of Radio Button and Checkbox

import PySimpleGUI as psg
#set the theme for the screen/window
psg.theme('TealMono')
#define layout
layout=[[psg.Text('Choose your Bread',size=(20, 1), font='Lucida',justification='left')],
        [psg.Radio('Whole Wheat','rd_bread', key ='Whole Wheat'), psg.Radio('Multigrain','rd_bread', key='Multigrain'),
         psg.Radio('Normal','rd_bread', key='Normal'),psg.Radio('Stuffed','rd_bread', key='Stuffed'),psg.Radio('Healthy seeds','rd_bread', key='Healthy seeds')],
        [psg.Text('Choose your Toppings',size=(20, 1), font='Lucida',justification='left')],
        [psg.Checkbox('Pepperoni',key='Pepperoni'), psg.Checkbox('Mushroom',key='Mushroom'),
         psg.Checkbox('Corn',key='Corn'),psg.Checkbox('Cherry Tomatoes',key='Cherry Tomatoes'),psg.Checkbox('Olives',key='Olives')],
        [psg.Text('Choose your Sauces',size=(20, 1), font='Lucida',justification='left')],
        [psg.Checkbox('Onion',key='Onion Sauce'), psg.Checkbox('Paprika',key='Paprika'),
         psg.Checkbox('Schezwan',key='Schezwan'),psg.Checkbox('Tandoori',key='Tandoori')],
        [psg.Button('SAVE', font=('Times New Roman',12)),psg.Button('CANCEL', font=('Times New Roman',12))]]
#Define Window
win =psg.Window('Make your Pizza',layout)
#Read  values entered by user
e,v=win.read()
#close first window
win.close()
#access all the values and if selected add them to a string
strx=""
for val in v:
    if win.FindElement(val).get()==True:
        strx=strx+ " "+ val+","
#display string in a popup         
psg.popup('Custom Pizza',      
            'Your chosen pizza will be made with', strx[0:len(strx)-1]) 
Python GUI Example
Output

3 Comments

  1. Mike said:

    I hope a few comments will be ok.
    A few things in the example program:
    * After a window is closed, you should no longer use that window in any fashion. No calls to methods.
    * Rather than using FindElement, use the [ ] style key lookup
    * It would be great if you could use the naming conventions recommended like event, values so that your code is consistent with the rest of the PySimpleGUI system. It’ll be more descriptive for your readers too to see “event” instead of “e”, sg instead of psg, etc. The coding conventions were documented and published with the hopes of maintaining some level of consistency across programs, especially something like tutorials.
    * The values dictionary should be used to get the values of all of the inputs from a window. The “get” method is not meant to be used in this way. You’re calling it after the window has been closed which is also risky (see above).

    You did a great job of writing descriptions for the parameters. I expected them to be copies of the descriptions from the published documentation http://Calls.PySimpleGUI.org but they are not. You wrote them yourself, at least the few I checked.

    February 14, 2021
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *