为什么按钮 "Add" 只工作一次?
Why is the button "Add" only working once?
我希望能够多次使用“添加”按钮,但目前它只能使用一次。
我只导入了 pysimplegui。
这是代码的一部分,因为看起来我有字符限制或其他限制。
希望我不用再等三天了...
[sg.Text("Index")],
[sg.Listbox(load_list(), size=(17,16), key='-LISTBOX-')],
[sg.Button("Add", key='-ADD-'), sg.Button("Delete", key='-DELETE-')]
]
window = sg.Window("Index", layout)
add_window = sg.Window('Add a name', adicionar_layout)
# Event Loop
while True:
event, values = window.read()
if event is None:
break
if event == '-ADD-':
add, input = add_window.read()
if add == '-SUBMIT-':
new_name = input.get('-INPUTADD-')
with open('saved_names.txt', 'a') as f:
f.write('\n' + new_name)
window.Element('-LISTBOX-').Update((load_list()))
add_window.close()
print(load_list())
window.close()
您的 add_window
在第一次活动 -ADD-
后关闭,因此在它之后无法使用。
添加一个新的弹出窗口以获取新名称和 return 输入值到主 window。
import PySimpleGUI as sg
def popup_get_new_name():
layout = [
[sg.Text("New Name"), sg.Input(key='INPUT')],
[sg.Button("Submit"), sg.Button('Cancel')],
]
window = sg.Window("New Name", layout, modal=True)
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, "Cancel"):
new_name = None
elif event == "Submit":
new_name = values['INPUT']
break
window.close()
return new_name
sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 12))
layout = [
[sg.Button("Add")],
[sg.StatusBar("", size=(40, 1), key='STATUS')],
]
window = sg.Window('Add New Name', layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'Add':
new_name = popup_get_new_name()
if new_name is not None:
window['STATUS'].update(value=new_name)
window.close()
我希望能够多次使用“添加”按钮,但目前它只能使用一次。 我只导入了 pysimplegui。 这是代码的一部分,因为看起来我有字符限制或其他限制。 希望我不用再等三天了...
[sg.Text("Index")],
[sg.Listbox(load_list(), size=(17,16), key='-LISTBOX-')],
[sg.Button("Add", key='-ADD-'), sg.Button("Delete", key='-DELETE-')]
]
window = sg.Window("Index", layout)
add_window = sg.Window('Add a name', adicionar_layout)
# Event Loop
while True:
event, values = window.read()
if event is None:
break
if event == '-ADD-':
add, input = add_window.read()
if add == '-SUBMIT-':
new_name = input.get('-INPUTADD-')
with open('saved_names.txt', 'a') as f:
f.write('\n' + new_name)
window.Element('-LISTBOX-').Update((load_list()))
add_window.close()
print(load_list())
window.close()
您的 add_window
在第一次活动 -ADD-
后关闭,因此在它之后无法使用。
添加一个新的弹出窗口以获取新名称和 return 输入值到主 window。
import PySimpleGUI as sg
def popup_get_new_name():
layout = [
[sg.Text("New Name"), sg.Input(key='INPUT')],
[sg.Button("Submit"), sg.Button('Cancel')],
]
window = sg.Window("New Name", layout, modal=True)
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, "Cancel"):
new_name = None
elif event == "Submit":
new_name = values['INPUT']
break
window.close()
return new_name
sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 12))
layout = [
[sg.Button("Add")],
[sg.StatusBar("", size=(40, 1), key='STATUS')],
]
window = sg.Window('Add New Name', layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'Add':
new_name = popup_get_new_name()
if new_name is not None:
window['STATUS'].update(value=new_name)
window.close()