嵌套时 PysimpleGui 按钮不起作用
PysimpleGui button not working when nested
这是我的代码:
import PySimpleGUI as sg
layout1 = [[sg.Button('New', "center")], [sg.Button("Load Save", "center")]]
layout2 = [[sg.Text("2nd Page")], [sg.Button("New Page", "center")], [sg.Button("Load Page", "center")]]
layout = [[sg.Column(layout1, key='-COL1-'), sg.Column(layout2, visible=False, key='-COL2-')]]
window = sg.Window('ORIGINAL').Layout(layout)
while True: # Event Loop
event, values = window.Read()
if event in (None, 'Exit'):
break
if event == 'New':
print("hello")
elif event == '2':
print("hello 2")
window.Close()
当我点击按钮时没有任何反应。
如果我将这些按钮放在布局中(而不是 layout1 或 layout2),它们就可以完美工作。
所以我的问题是如何让这些按钮正常工作,同时将它们留在原处?
对不起,如果这是一个愚蠢的问题,我是 pysimplegui 的新手
问题不在于按钮元素的位置,而在于您向 sg.Button
提供的参数。
sg.Button
定义为
class Button(Element):
"""
Button Element - Defines all possible buttons. The shortcuts such as Submit, FileBrowse, ... each create a Button
"""
def __init__(self, button_text='', button_type=BUTTON_TYPE_READ_FORM, target=(None, None), tooltip=None,
file_types=(("ALL Files", "*.*"),), initial_folder=None, default_extension='', disabled=False, change_submits=False,
enable_events=False, image_filename=None, image_data=None, image_size=(None, None),
image_subsample=None, border_width=None, size=(None, None), s=(None, None), auto_size_button=None, button_color=None,
disabled_button_color=None,
highlight_colors=None, mouseover_colors=(None, None), use_ttk_buttons=None, font=None, bind_return_key=False, focus=False, pad=None, key=None,
k=None, right_click_menu=None, expand_x=False, expand_y=False, visible=True, metadata=None):
当您将参数传递给 sg.Button
时省略了关键字,第二个参数将是 button_type
,不确定您为什么给出 'center'
。除了 button_text
、key
或 key
之外,显然没有任何其他参数可以具有这样的值 'center'
.
import PySimpleGUI as sg
layout1 = [
[sg.Button('New')],
[sg.Button('Load')],
[sg.Button('Save')],
]
layout2 = [
[sg.Text("2nd Page")],
[sg.Button("New Page")],
[sg.Button("Load Page")],
]
layout = [
[sg.Column(layout1, key='-COL1-'),
sg.Column(layout2, visible=False, key='-COL2-')],
]
window = sg.Window('ORIGINAL', layout)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'New':
print("Hello New")
elif event == 'Load':
print("Hello Load")
elif event == 'Save':
print("Hello Save")
window.close()
这是我的代码:
import PySimpleGUI as sg
layout1 = [[sg.Button('New', "center")], [sg.Button("Load Save", "center")]]
layout2 = [[sg.Text("2nd Page")], [sg.Button("New Page", "center")], [sg.Button("Load Page", "center")]]
layout = [[sg.Column(layout1, key='-COL1-'), sg.Column(layout2, visible=False, key='-COL2-')]]
window = sg.Window('ORIGINAL').Layout(layout)
while True: # Event Loop
event, values = window.Read()
if event in (None, 'Exit'):
break
if event == 'New':
print("hello")
elif event == '2':
print("hello 2")
window.Close()
当我点击按钮时没有任何反应。 如果我将这些按钮放在布局中(而不是 layout1 或 layout2),它们就可以完美工作。 所以我的问题是如何让这些按钮正常工作,同时将它们留在原处?
对不起,如果这是一个愚蠢的问题,我是 pysimplegui 的新手
问题不在于按钮元素的位置,而在于您向 sg.Button
提供的参数。
sg.Button
定义为
class Button(Element):
"""
Button Element - Defines all possible buttons. The shortcuts such as Submit, FileBrowse, ... each create a Button
"""
def __init__(self, button_text='', button_type=BUTTON_TYPE_READ_FORM, target=(None, None), tooltip=None,
file_types=(("ALL Files", "*.*"),), initial_folder=None, default_extension='', disabled=False, change_submits=False,
enable_events=False, image_filename=None, image_data=None, image_size=(None, None),
image_subsample=None, border_width=None, size=(None, None), s=(None, None), auto_size_button=None, button_color=None,
disabled_button_color=None,
highlight_colors=None, mouseover_colors=(None, None), use_ttk_buttons=None, font=None, bind_return_key=False, focus=False, pad=None, key=None,
k=None, right_click_menu=None, expand_x=False, expand_y=False, visible=True, metadata=None):
当您将参数传递给 sg.Button
时省略了关键字,第二个参数将是 button_type
,不确定您为什么给出 'center'
。除了 button_text
、key
或 key
之外,显然没有任何其他参数可以具有这样的值 'center'
.
import PySimpleGUI as sg
layout1 = [
[sg.Button('New')],
[sg.Button('Load')],
[sg.Button('Save')],
]
layout2 = [
[sg.Text("2nd Page")],
[sg.Button("New Page")],
[sg.Button("Load Page")],
]
layout = [
[sg.Column(layout1, key='-COL1-'),
sg.Column(layout2, visible=False, key='-COL2-')],
]
window = sg.Window('ORIGINAL', layout)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'New':
print("Hello New")
elif event == 'Load':
print("Hello Load")
elif event == 'Save':
print("Hello Save")
window.close()