Pysimplegui 简明教程
PySimpleGUI - Events
任何 GUI 应用程序都是事件驱动的,它有能力响应 GUI 元素上发生的各种可能的事件。在 PySimpleGUI 中,事件处理在 GUI 设计的结构下面一个无限循环中完成,不断检查是否发生了事件并根据事件执行动作。
Any GUI application is event driven, having the ability to respond to the various possible events occurring on the GUI elements. In PySimpleGUI, the event handling 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.
窗口事件默认启用,包括按钮事件(当单击任何按钮时发生)和标题栏上“X”按钮的事件。
The window events are enabled by default, and include the button events (occur when any button is clicked) and the event of the "X" button on the titlebar clicked.
元素事件默认不会启用。只有当在创建元素时将“enable_events”参数设置为 True 时,才能检测到特定于元素的事件。
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
一个无限事件循环使 PySimpleGUI 窗口持久存在,当用户按下“X”按钮或执行 Window 类的 close() 方法时,该循环将终止。终止循环的标准方法如下 −
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()
如果此参数设置为 True,Widow 类还将发出 “enable_close_attempted_event”。当在循环内部检测到时,调用是/否弹出窗口是一种好习惯。
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
在这种情况下,当按下“X”按钮时,带有是/否按钮的弹出窗口将出现,当单击“是”按钮时程序将退出。
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 clicked.
它将生成以下输出窗口 −
It will produce the following output window −
事件值还返回“-WINDOW CLOSE ATTEMPTED-”值。
The event value also returns the "-WINDOW CLOSE ATTEMPTED-" value.
-WINDOW CLOSE ATTEMPTED- {'-FIRST-': '200', '-SECOND-': '300'}
Button Events
按钮单击事件默认启用。要禁用,请使用“Button.update(disabled=True)”。您还可以在按钮的构造函数中设置“enable_events=True”,它将启用 Button Modified 事件。当有内容“写入”到按钮时触发此事件。
The button click 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.
当我们读取窗口的内容(使用“window.read()”)时,按钮值将是其标题(如果未设置键)或已设置则为键。
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.
在上述示例中,由于未在 Add 和 Sub 按钮上设置键参数,因此在读取窗口时将返回它们的标题。
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 和 Sub 按钮添加键参数。
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()
read() 方法返回的元组现在将显示按下的按钮的键。
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 slider is moved, or an item from the list is selected on or a radio button is clicked on.
与 Button 或 Window 不同,这些事件默认不会启用。要为元素启用事件,请将参数“enable_events=True”设置为。
Unlike 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 |
Multiline |
any key pressed |
Text |
Clicked |
Status Bar |
Clicked |
Graph |
Clicked |
Graph |
Dragged |
Graph |
drag ended (mouse up) |
TabGroup |
tab clicked |
Slider |
slider moved |
Table |
row selected |
Tree |
node selected |
ButtonMenu |
menu item chosen |
Right click menu |
menu item chosen |