如何使用 while 循环 PySimpleGUI 中的键获取日历按钮的值?
How can I get Calendar Button's value using its key inside the while loop, PySimpleGUI?
我是 PySimpleGUI 的新手。我正在为桌面应用程序构建 GUI,并想使用日历。但是当我从日历选择器弹出窗口中选择时,在 window.read() 中没有超时的 while 循环中检索日历按钮事件的值时出现了问题。我尝试使用 event == 'CalendarButton's text' 获取它的值,但不能,尽管如果您选择设置不同的日期,它的按钮文本每次都会更改。你能帮忙解决这个问题吗,或者我如何在 while 循环中使用它的键来获取它的(或任何元素的)值。或者至少我可以使用 Tkinter 日历包。如果是这样,我如何将 Tkinter 日历与 PySimpleGUI 连接 window,我是否必须将元素的键绑定与 Tkinter 一起使用?
这是我放入 window 布局中的日历按钮定义代码:
sg.CalendarButton('Calendar', target='_CALENDAR_', pad=None, font=('MS Sans Serif', 10, 'bold'),
button_color=('red', 'white'), key='_CALENDAR_', format=('%d %B, %Y'))
and here's the while loop events handling part
# LOOP EVENTS #
while True:
# READ EVENT VALUES #
event, values = window.read()
# EXIT OR CLOSE EVENT #
if event is None or event == 'Exit':
break
# BUTTON EVENTS
# CALENDAR BUTTON CLICKED EVENT #
if event == 'Calendar':
window['_CALENDAR_'](disabled=True)
# CLOSE WINDOW
window.close()
# POPUP
sg.Popup('Title',
'The results of the window.',
'The button clicked was "{}"'.format(event),
'The values are', values)
此外,退出 window
后,我在 sg.Popup() 输出中看不到此事件的值
'EDITED' 抱歉,sg.Popup() 中有错误。现在修复它。
同时保存值和获取事件的方法是创建一个隐藏的输入字段。为输入字段启用事件,当日历填充您设置为目标的输入字段时,您将获得一个事件。
import PySimpleGUI as sg
layout = [ [sg.Text('Calendar example')],
[sg.In(key='-CAL-', enable_events=True, visible=False), sg.CalendarButton('Calendar', target='-CAL-', pad=None, font=('MS Sans Serif', 10, 'bold'),
button_color=('red', 'white'), key='_CALENDAR_', format=('%d %B, %Y'))],
[sg.Exit()]]
window = sg.Window('Calendar', layout)
while True: # Event Loop
event, values = window.read()
print(event, values)
if event in (None, 'Exit'):
break
window.close()
我是 PySimpleGUI 的新手。我正在为桌面应用程序构建 GUI,并想使用日历。但是当我从日历选择器弹出窗口中选择时,在 window.read() 中没有超时的 while 循环中检索日历按钮事件的值时出现了问题。我尝试使用 event == 'CalendarButton's text' 获取它的值,但不能,尽管如果您选择设置不同的日期,它的按钮文本每次都会更改。你能帮忙解决这个问题吗,或者我如何在 while 循环中使用它的键来获取它的(或任何元素的)值。或者至少我可以使用 Tkinter 日历包。如果是这样,我如何将 Tkinter 日历与 PySimpleGUI 连接 window,我是否必须将元素的键绑定与 Tkinter 一起使用?
这是我放入 window 布局中的日历按钮定义代码:
sg.CalendarButton('Calendar', target='_CALENDAR_', pad=None, font=('MS Sans Serif', 10, 'bold'),
button_color=('red', 'white'), key='_CALENDAR_', format=('%d %B, %Y'))
and here's the while loop events handling part
# LOOP EVENTS #
while True:
# READ EVENT VALUES #
event, values = window.read()
# EXIT OR CLOSE EVENT #
if event is None or event == 'Exit':
break
# BUTTON EVENTS
# CALENDAR BUTTON CLICKED EVENT #
if event == 'Calendar':
window['_CALENDAR_'](disabled=True)
# CLOSE WINDOW
window.close()
# POPUP
sg.Popup('Title',
'The results of the window.',
'The button clicked was "{}"'.format(event),
'The values are', values)
此外,退出 window
后,我在 sg.Popup() 输出中看不到此事件的值'EDITED' 抱歉,sg.Popup() 中有错误。现在修复它。
同时保存值和获取事件的方法是创建一个隐藏的输入字段。为输入字段启用事件,当日历填充您设置为目标的输入字段时,您将获得一个事件。
import PySimpleGUI as sg
layout = [ [sg.Text('Calendar example')],
[sg.In(key='-CAL-', enable_events=True, visible=False), sg.CalendarButton('Calendar', target='-CAL-', pad=None, font=('MS Sans Serif', 10, 'bold'),
button_color=('red', 'white'), key='_CALENDAR_', format=('%d %B, %Y'))],
[sg.Exit()]]
window = sg.Window('Calendar', layout)
while True: # Event Loop
event, values = window.read()
print(event, values)
if event in (None, 'Exit'):
break
window.close()