English 中文(简体)
PySimpleGUI - Events
  • 时间:2025-02-05

PySimpleGUI - Events


Previous Page Next Page  

Any GUI apppcation is event driven, having the abipty to respond to the various possible events occurring on the GUI elements. In PySimpleGUI, the event handpng is done inside an infinite loop below the constitution of GUI design, continuously checking whether an event occurs and perform the actions based on the event.

There are two types of events −

    Window events, and

    Element events.

The window events are enabled by default, and include the button events (occur when any button is cpcked) and the event of the "X" button on the titlebar cpcked.

The element events are not enabled by default. Element-specific events can be detected only when the "enable_events" parameter is set to True when an element is created.

Window Closed Event

The infinite event loop that makes the PySimpleGUI window persistent, is terminated when the user presses the "X" button, or the close() method of Window class is executed. The standard way of terminating the loop is as follows −


import PySimpleGUI as psg
...
while True:
 ...

   if event == psg.WIN_CLOSED:
      break
 ...
window.close()

The Widow class also emits an "enable_close_attempted_event" if this parameter is set to True. It is a good practice to call yes-no popup when it is detected inside the loop.


window = psg.Window( Calculator , layout,  enable_close_attempted_event=True)
while True:
   event, values = window.read()
   print(event, values)
   if event == "Add":
      result = int(values[ -FIRST- ]) + int(values[ -SECOND- ])
   if event == "Sub":
      result = int(values[ -FIRST- ]) - int(values[ -SECOND- ])
   window[ -OUT- ].update(result)
   if event == psg.WINDOW_CLOSE_ATTEMPTED_EVENT and psg.popup_yes_no( Do you really want to exit? ) ==  Yes :
      break
   if event == psg.WIN_CLOSED or event ==  Exit :
      break

In this case, as the "X" button is pressed, the Popup with Yes/No button appears and the program exits when the "Yes" button is cpcked.

It will produce the following output window −

Window Closed Event

The event value also returns the "-WINDOW CLOSE ATTEMPTED-" value.


-WINDOW CLOSE ATTEMPTED- { -FIRST- :  200 ,  -SECOND- :  300 }

Button Events

The button cpck event is enabled by default. To disable, use "Button.update(disabled=True)". You can also set "enable_events=True" in Button’s constructor, it will enable the Button Modified event. This event is triggered when something writes to a button.

When we read the contents of the window (using "window.read()"), the button value will be either its caption (if key is not set) or key if it is set.

In the above example, since the key parameter is not set on the Add and Sub buttons, their captions are returned when the window is read.


Add { -FIRST- :  200 ,  -SECOND- :  300 }

Add key parameters to Add and Sub buttons in the program.


import PySimpleGUI as psg
layout = [
   [psg.Text( Enter a num:  ), psg.Input(key= -FIRST- )],
   [psg.Text( Enter a num:  ), psg.Input(key= -SECOND- )],
   [psg.Text( Result :  ), psg.Text(key= -OUT- )],
   [psg.Button("Add", key= -ADD- ), psg.Button("Sub", key= - SUB- ), psg.Exit()],
]
window = psg.Window( Calculator , layout)
while True:
   event, values = window.read()
   print(event, values)

   if event == "-ADD-":
      result = int(values[ -FIRST- ]) + int(values[ -SECOND- ])

   if event == "-SUB-":
      result = int(values[ -FIRST- ]) - int(values[ -SECOND- ])

   window[ -OUT- ].update(result)

   if event == psg.WIN_CLOSED or event ==  Exit :
      break
window.close()

The tuple returned by the read() method will now show the key of button pressed.


-ADD- { -FIRST- :  200 ,  -SECOND- :  300 }

Events of Other Elements

Many of the elements emit events when some type of user interaction takes place. For example, when a spder is moved, or an item from the pst is selected on or a radio button is cpcked on.

Unpke Button or Window, these events are not enabled by default. To enable events for an Element, set the parameter "enable_events=True".

The following table shows the elements and the events generated by them.

Name Events
InputText any key pressed
Combo item selected
Listbox selection changed
Radio selection changed
Checkbox selection changed
Spinner new item selected
Multipne any key pressed
Text Cpcked
Status Bar Cpcked
Graph Cpcked
Graph Dragged
Graph drag ended (mouse up)
TabGroup tab cpcked
Spder spder moved
Table row selected
Tree node selected
ButtonMenu menu item chosen
Right cpck menu menu item chosen
Advertisements