如何在 PySimpleGUI 中为额外的 window 设置独占的“事件”和“值”
How to set exclusive `event` and `values` for an additional window in PySimpleGUI
我制作了一个按钮,点击它会出现一个新的 window。如何为这个 window 设置独占 event
和 values
?定义像 event0
和 values0
这样的新变量就足够了吗?就像是:
event0, values0 = window0.read()
。谢谢。
这里是两个windows同时出现的例子:
import PySimpleGUI as sg
"""
Demo - 2 simultaneous windows using read_all_window
Both windows are immediately visible. Each window updates the other.
Copyright 2020 PySimpleGUI.org
"""
def make_win1():
layout = [[sg.Text('Window 1')],
[sg.Text('Enter something to output to Window 2')],
[sg.Input(key='-IN-', enable_events=True)],
[sg.Text(size=(25,1), key='-OUTPUT-')],
[sg.Button('Reopen')],
[sg.Button('Exit')]]
return sg.Window('Window Title', layout, finalize=True)
def make_win2():
layout = [[sg.Text('Window 2')],
[sg.Text('Enter something to output to Window 1')],
[sg.Input(key='-IN-', enable_events=True)],
[sg.Text(size=(25,1), key='-OUTPUT-')],
[sg.Button('Exit')]]
return sg.Window('Window Title', layout, finalize=True)
def main():
window1, window2 = make_win1(), make_win2()
window2.move(window1.current_location()[0], window1.current_location()[1]+220)
while True: # Event Loop
window, event, values = sg.read_all_windows()
if window == sg.WIN_CLOSED: # if all windows were closed
break
if event == sg.WIN_CLOSED or event == 'Exit':
window.close()
if window == window2: # if closing win 2, mark as closed
window2 = None
elif window == window1: # if closing win 1, mark as closed
window1 = None
elif event == 'Reopen':
if not window2:
window2 = make_win2()
window2.move(window1.current_location()[0], window1.current_location()[1] + 220)
elif event == '-IN-':
output_window = window2 if window == window1 else window1
if output_window: # if a valid window, then output to it
output_window['-OUTPUT-'].update(values['-IN-'])
else:
window['-OUTPUT-'].update('Other window is closed')
if __name__ == '__main__':
main()
您调用 sg.read_all_windows() 而不是两次 window 读取。阅读 window、事件和 window 的值。如果您有多个 window.
,这将非常灵活
我制作了一个按钮,点击它会出现一个新的 window。如何为这个 window 设置独占 event
和 values
?定义像 event0
和 values0
这样的新变量就足够了吗?就像是:
event0, values0 = window0.read()
。谢谢。
这里是两个windows同时出现的例子:
import PySimpleGUI as sg
"""
Demo - 2 simultaneous windows using read_all_window
Both windows are immediately visible. Each window updates the other.
Copyright 2020 PySimpleGUI.org
"""
def make_win1():
layout = [[sg.Text('Window 1')],
[sg.Text('Enter something to output to Window 2')],
[sg.Input(key='-IN-', enable_events=True)],
[sg.Text(size=(25,1), key='-OUTPUT-')],
[sg.Button('Reopen')],
[sg.Button('Exit')]]
return sg.Window('Window Title', layout, finalize=True)
def make_win2():
layout = [[sg.Text('Window 2')],
[sg.Text('Enter something to output to Window 1')],
[sg.Input(key='-IN-', enable_events=True)],
[sg.Text(size=(25,1), key='-OUTPUT-')],
[sg.Button('Exit')]]
return sg.Window('Window Title', layout, finalize=True)
def main():
window1, window2 = make_win1(), make_win2()
window2.move(window1.current_location()[0], window1.current_location()[1]+220)
while True: # Event Loop
window, event, values = sg.read_all_windows()
if window == sg.WIN_CLOSED: # if all windows were closed
break
if event == sg.WIN_CLOSED or event == 'Exit':
window.close()
if window == window2: # if closing win 2, mark as closed
window2 = None
elif window == window1: # if closing win 1, mark as closed
window1 = None
elif event == 'Reopen':
if not window2:
window2 = make_win2()
window2.move(window1.current_location()[0], window1.current_location()[1] + 220)
elif event == '-IN-':
output_window = window2 if window == window1 else window1
if output_window: # if a valid window, then output to it
output_window['-OUTPUT-'].update(values['-IN-'])
else:
window['-OUTPUT-'].update('Other window is closed')
if __name__ == '__main__':
main()
您调用 sg.read_all_windows() 而不是两次 window 读取。阅读 window、事件和 window 的值。如果您有多个 window.
,这将非常灵活