Web Analytics Made Easy - Statcounter
Skip to content

Make the Window Do Something Useful

Recall that taking our "fresh eyes" approach to GUIs, we broke GUIs into 2 steps:

  1. Create the window
  2. Make the window do something useful

In the previous section you learned how to create the window, now let's do something useful with it.

What Do Windows Do?

Putting our "fresh eyes" back into our face, let's look at what a window does.

We can simplify what windows do as these 3 things

  1. Collects input data
  2. Gets "events" (user interactions)
  3. Displays output data

"Reading" Windows

The Window object has a method called read you call in order to perform the first 2 items on the list - collect input data and get the "event" that happened.

When you call window.read() it returns 2 items to you:

  1. The event that happened
  2. A Dictionary containing all of the input values in your window

Instead of "asking" for the value of each input element (checkbox for example), you are instead "told. This is different than most GUI frameworks. PySimpleGUI simplified the operation by putting all input values into a dictionary so that you don't have to ask one by one. Wasn't that nice of PySimpleGUI?

The Python code for this operation, using the suggested coding conventions, is written as:

event, values = window.read()

Closing a Window

When you've completed working with a Window and want to close it, you call the close method for your Window object.


Let's Do It!

Combining all of the code so far, we end up with this program:

import PySimpleGUI as sg

layout = [  [sg.Text('ROW 1'), sg.Button('Row 1 - #1'), sg.Checkbox('Row 1 - #2'), sg.Button('Row 1 - #3')],
            [sg.Text('ROW 2'), sg.Checkbox('Row 2 - #1'), sg.Checkbox('Row 2 - #2'), sg.Checkbox('Row 2 - #3')],
            [sg.Text('ROW 3'), sg.Button('Row 3 - #1'), sg.Button('Row 3 - #2')]  ]

window = sg.Window('Window Title', layout)

event, values = window.read()


Give it a go! Click Run! You will see the window we created. It will remain visible until you either click a button or close the window with the "X".