Web Analytics Made Easy - Statcounter
Skip to content

Function and Aliases

This is related to the topic of "User Defined Elements" if you care to go look it up.

If you're using PyCharm, this technique works particuarly well because the DocStrings continue to work even after you have created aliases.

Aliases are used a LOT in PySimpleGUI. You'll find that nearly all of the Elements have multiple names that can be used for them. Text Elements can be specified as Text, Txt, and T. This allows you to write really compact code.

You can make your own aliases too. The advantage of you making your own is that they will be in your own name space and thus will not have the typical sg. in front of them.

Let's use the cprint function as an example.

Normally you'll call this function like this:

sg.cprint('This is my white text on a red background', colors='white on red')

If you have a lot of these in your program, it won't get too long until you're tired of typing sg.cprint, so, why not make it super easy on yourself and type cp instead. Here's all you have to do.

cp = sg.cprint

cp('This is my white text on a red background', colors='white on red')

Running these 2 calls produced these 2 lines of text in a Multiline element

image

IF you're using PyCharm and press Control+Q with your cursor over the cp, you'll see the documentation brought up for the cprint call:

image

Feel free to experiment. Even renaming elements will save you the hassle of typing in the sg. portion. Then again, so will importing the invdividual elements.

# this import will allow you to type just "Text" to use a Text Element
from PySimpleGUI import Text

layout = [[Text('Simpler looking layout')]]