其他分享
首页 > 其他分享> > pysimplegui多窗口设置

pysimplegui多窗口设置

作者:互联网

# -*- coding: utf-8 -*-
"""
Created on Mon Sep 12 11:39:42 2022

@author: Administrator
"""

import PySimpleGUI as sg
 
# Design pattern 2 - First window remains active
# 多窗口设计模式 1 - 两个窗口都处于活动状态
layout = [[ sg.Text('Window 1'),],
          [sg.Input(do_not_clear=True)],
          [sg.Text(size=(15,1), key='-OUTPUT-')],
          [sg.Button('Launch 2'), sg.Button('Exit')]]
 
win1 = sg.Window('Window 1', layout)
 
win2_active = False
while True:
    ev1, vals1 = win1.read(timeout=100)
    win1['-OUTPUT-'].update(vals1[0])
    if ev1 == sg.WIN_CLOSED or ev1 == 'Exit' or ev1 == None:
        break
    
    if not win2_active and ev1 == 'Launch 2':
        win2_active = True
        layout2 = [[sg.Text('Window 2')],
                   [sg.Button('Exit')]]
 
        win2 = sg.Window('Window 2', layout2)
 
    if win2_active:
        ev2, vals2 = win2.read(timeout=100)
        if ev2 == sg.WIN_CLOSED or ev2 == 'Exit'  or ev2 == None:
            win2_active  = False
            win2.close()

win1.close()



import PySimpleGUI as sg
# Design pattern 1 - First window does not remain active
# 多窗口设计模式 2 - 只有 1 个活动窗口
layout = [[ sg.Text('Window 1'),],
          [sg.Input(do_not_clear=True)],
          [sg.Text(size=(15,1), key='-OUTPUT-')],
          [sg.Button('Launch 2'), sg.Button('Exit')]]
 
win1 = sg.Window('Window 1', layout)
 
win2_active = False
while True:
    ev1, vals1 = win1.read(timeout=100)
    win1['-OUTPUT-'].update(vals1[0])
    if ev1 == sg.WIN_CLOSED or ev1 == 'Exit' or ev1 == None:
        break
    
    if not win2_active and ev1 == 'Launch 2':
        win2_active = True
        layout2 = [[sg.Text('Window 2')],
                   [sg.Button('Exit')]]
 
        win2 = sg.Window('Window 2', layout2)
        win1.hide()
 
    if win2_active:
        ev2, vals2 = win2.read(timeout=100)
        if ev2 == sg.WIN_CLOSED or ev2 == 'Exit'  or ev2 == None:
            win2_active  = False
            win2.close()
            win1.un_hide()

win1.close()




# 多窗口设计模式 2 - 只有 1 个活动窗口
import PySimpleGUI as sg

def make_window1():
    layout = [[sg.Text('Window 1'), ],
              [sg.Input(key='-IN-')],
              [sg.Text(size=(20, 1), key='-OUTPUT-')],
              [sg.Button('Launch 2'), sg.Button('Output')]]

    return sg.Window('Window 1', layout, finalize=True)

def make_window2():
    layout = [[sg.Text('Window 2')],
               [sg.Button('Exit')]]
    return sg.Window('Window 2', layout, finalize=True)


def main():
    window2 = None
    window1 = make_window1()

    while True:
        window, event, values = sg.read_all_windows()
        if event == sg.WIN_CLOSED and window == window1:
            break

        if window == window1:
            window1['-OUTPUT-'].update(values['-IN-'])

        if event == 'Launch 2' and not window2:
            window1.hide()
            window2 = make_window2()

        if window == window2 and (event in (sg.WIN_CLOSED, 'Exit')):
            window2.close()
            window2 = None
            window1.un_hide()
    window1.close()

main()            

  

 

标签:win1,win2,多窗口,pysimplegui,Window,Exit,设置,active,sg
来源: https://www.cnblogs.com/andylhc/p/16686100.html