Debug Print
import PySimpleGUI as sg
sg.Print('Hello, World of GUIs!')
sg.Print('While I have your attention...', end='', colors='white on red')
sg.Print('\nsg.Print looks much like the Python ', end='')
sg.Print('print', colors='white on green', end='')
sg.Print(' statement.')
sg.Print('But it also includes FUN additions like colors', text_color='purple')
sg.Print('As well a "wait" parameter to stop the program from exiting.', wait=True)
After the 1978 publication of the iconic "The C Programming Language" by Brian Kerninghan and Dennis Ritchie, almost every computer programming book begins with a "hello world program", A 1-line program that demonstrates the environment is properly set up and you're about to see the output.
It's cleverly deceptively simple. While not logically important to know this knowledge at this point in your PySimpleGUI education, let's follow this 43 year tradition as it does force us to look at the Python and computer world differently than most Python programmers view it.
"print" but to where?
In the normal world of Python programs, the "hello world program" would be:
You would run this program via the "command line" which would look something like this:
Your PySimpleGUI program can also use plain print
statements and the output will also appear in the "console" in addition to the window that has the output of our sg.Print
statements, the "Debug Print" window.
So far, no surprises. It's when we get to the "corner cases" that the problem gets interesting. "Corner cases" is a computer science term defined as "a situation that occurs outside the normal operating procedures". In other words, something weird happens.
No Console
Two simple corner cases for this print example are:
- The .py is double clicked to launch it
- Instead of a .py file, the hello_world program is renamed to be a .pyw file and launched using pythonw which does not create any console
In the first case, as soon as the program exits, the console window disappears. In the second, a console window is never created.
By the way, there are other ways to handle "print" output in PySimpleGUI, at this point, we're talking about a conceptual "print" that requires no additional setup on your part. You, as a beginning programmer, simply want to print "hello world" as your very first program.
The PySimpleGUI "Debug Print Window" was designed specifically for this situation. You want to print using a simple, 1 line statement. No problem, we've got you covered. PySimpleGUI creates a window that serves as a destination for your "prints" to the Debug Window. It's kind of like a virtual console for output purposes.
When a GUI Program Exits
When your PySimpleGUI program exits, this is true for GUI programs in general, all of the windows will close (and thus disappear). This is why double clicking a .py file with prints results in a console window being shown and then quickly disappearing.
To solve this problem, what you want is a "wait" capability.... and that's exactly what the sg.Print
call gives you in the form of the wait
parameter. Adding wait=True
to your call adds the "Click to Continue..." button you see in the screenshot. The call to sg.Print
will wait until you clock the button before returning, thus delaying the program's exit.
sg.Print
- Fun, interesting, unique, but not commonly used
While sg.Print
was a good place for us to start, particularly since it's how most programming books start, it's not a commonly used capability of PySimpleGUI.
More commonly used is the cprint
call which allows you to "print" using multiple colors to Multiline elements in your window.