PySimpleGUI – Themes, Text, Input, Multiline and Button

In this section of PySimpleGUI tutorial we will discuss and learn to use themes, Text, Input, Multiline and Button.

Theme

You can choose from a range of themes to pick for your form’s look and feel.  theme() function can be called with the package instance to set the look. It is called as below

import PsSimpleGUI as psg
psg.theme(“name of the theme”)

To know which theme you wish to apply,  run theme_previewer()command (in Python command window)  and see the complete palette of the options available each identified by a name. Node the name of your preferred theme and write it as the string argument in theme() function.

Just take a look at your options you have got!!!

PySimpleGUI themes

It’s not like that once you choose a theme you are stuck with the font styles and  font provided by your chosen theme. You can customize the form’s look with these mentioned attributes and set their values according to the need of your GUI.

Attributes to be set for Text-Based elements

You can use the following attributes in all the text based elements of your layout or form or window. These are to be added as attribute=value pair separated by commas within the parentheses of the element you wish to customize.

Size

Change size of the text element by setting the width and height value pair for the element. It is defined in number of characters.

Size=(int width,int height)

Font

Customize the font of the text based element by setting the argument as string specifying the name of the font. 

Font=string font-family

Background Color

Set the background color of the element by specifying the name from the allowed Python colors for the background_color property.  It is given as a string

background_color= string color-name

Text Color

Modify the text color of the text based element by specifying the name from the allowed Python colors after the text_color property.  It is given as a string

text_color= string color-name

Text Justify

You can justify the text within an element using justification attribute.  It is given as a string and allowed values are left, right and center

justification= string justify-value

PySimpleGUI Elements – Text, Input, Multiline and Button

Text

The Text element allows you to print text on your Python program GUI. It can be used to print information for the user or display output. The syntax is

psg.Text(“text to display”, attribute-name=value, attribute-name=value…)

Input

To read a value from the user an input is used. It is usually associated with a text element and written as input() in the element list of the layout list for a window. You can modify the appearance of an input element by using previously discussed attributes.

[psg.Text(“text to display”, attribute-name=value, attribute-name=value…), Input(default text, attribute-name=value,…….)]

Multiline

For longer data like address, titles, remarks you may need to get the text from users in multiple lines so that users can view large text at the same time. Input displays only one line by default, so it is better to use the multiline element for such user input.

[psg.Text(“text to display”, attribute-name=value, attribute-name=value…), Multiline ()]

Button

Buttons are the elements which allow users to click for final submission of data or doing some other decision-like actions. If you want to create a set of buttons in a single row in your input form, declare all of them in single square parentheses separated by commas in the layout declaration.   This is because [] defines a list element of the layout list which is displayed in the next line in a window.

[psg.Button("Text of the Button" attribute-name=value, attribute-name=value…),….] 

Code Example

Here is an example that uses all the above mentioned PySimpleGUI elements. It’s a funny looking input and output, but the only reason of doing it in this way is that you get a clear idea of defining the elements and setting their attributes. In this example the user enters some input in one window and it is displayed as output in another window after closing the first (input) window.

import PySimpleGUI as psg
#set the theme for the screen/window
psg.theme("LightPurple")
#define layout
layout=[[psg.Text("Name",size=(15, 1), font='Lucida',justification='right'),psg.Input()],
        [psg.Text("Date of Birth",size=(15, 1), font=("Verdana",11),text_color='Black',background_color='Yellow', justification='right'),psg.Input()],
        [psg.Text("Class",size=(15, 1), font=("Verdana",11),text_color='Red',justification='right'),psg.Input('M.Sc', background_color='Yellow')],
        [psg.Text("Address",size=(15, 1), font=("Arial",11),text_color='Green',justification='right'),psg.Multiline()],
        [psg.Button("SAVE", font=("Times New Roman",12)),psg.Button("CANCEL", font=("Times New Roman",12))]]
#Define Window
win =psg.Window("Data Entry",layout)
#Read  values entered by user
e,v=win.read()
#close first window
win.close()

#define layout for second windows to display data entered by user in first window
layout1=[[psg.Text("The data you entered is  :", size=(20,1), font='Lucida', text_color='Magenta')],
        [psg.Text("Name :"+v[0], size=(20,1), font='Lucida', text_color='Blue')],
        [psg.Text("DOB :"+v[1], size=(20,1), font='Lucida', text_color='Lime')],
        [psg.Text("Class  :"+v[2], size=(len(v[2])+10,1), font='Lucida', text_color='Yellow')],
        [psg.Text("Address  :"+v[3], size=(int(len(v[3])/4),4), font='Lucida', text_color='Brown', justification='center')]]
#Define Window and display the layout to print output        
win1=psg.Window("Output Screen",layout1)

e,v=win1.read()
#close second window
win1.close()

Input Screen

Sample Code Input

Output Screen

Sample Code Output

Be First to Comment

Leave a Reply

Your email address will not be published.