如何在 PySimpleGUI 中一次全屏显示多个 windows

How to put multiple windows in fullscreen at once in PySimpleGUI

我正在尝试创建一些 windows,我需要它们全屏显示。我知道 .Maximize() 但我不能让它在这段代码中正常工作。有任何想法吗?谢谢!

import PySimpleGUI as sg

def make_window1():
    layout = [[sg.Text('Window 1')],
              [sg.Button('Next >'), sg.Button('Exit')]]

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


def make_window2():
    layout = [[sg.Text('Window 2')],
               [sg.Button('Next >'), sg.Button('Exit')]]

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


window1, window2 = make_window1(), None

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

    if window == window1:
        if event == 'Next >':
            window1.hide()
            window2 = make_window2()

    if window == window2 and event in (sg.WIN_CLOSED, 'Exit'):
        break


window.close()

完成后调用 window 的方法 maximize()

这里有四种方式来展示windows的切换。

  1. 使用不同的布局创建了新的 window。
import PySimpleGUI as sg

def make_window1():
    layout = [[sg.Text('Window 1')],
              [sg.Button('Next >'), sg.Button('Exit')]]
    window = sg.Window('Window 1', layout, finalize=True)
    window.maximize()
    return window

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

window, now = make_window1(), 1

while True:

    event, values = window.read()

    if event in (sg.WINDOW_CLOSED, 'Exit'):
        break
    elif event == 'Next >':
        win, now = (make_window1(), 1) if now == 2 else (make_window2(), 2)
        window.close()
        window = win

window.close()
  1. 双window同时创建并决定在屏幕上显示哪个。
import PySimpleGUI as sg

def make_window1():
    layout = [[sg.Text('Window 1')],
              [sg.Button('Next >'), sg.Button('Exit')]]
    window = sg.Window('Window 1', layout, finalize=True)
    return window


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

window1, window2 = make_window1(), make_window2()
window1.maximize()
window2.hide()

while True:

    window, event, values = sg.read_all_windows()

    if event in (sg.WINDOW_CLOSED, 'Exit'):
        break
    elif event == 'Next >':
        window.hide()
        window = window1 if window == window2 else window2
        window.un_hide()
        window.maximize()

window1.close()
window2.close()
  1. 使用 sg.Column 属性 visible True or False
import PySimpleGUI as sg

def swap(now, step):
    window[f'COLUMN {now}'].update(visible=False)
    now = (now+step) % total
    window[f'COLUMN {now}'].update(visible=True)
    return now

def column_layout(index):
    layout = [[sg.Text(f'Window {index+1}')]]
    return layout

total, now = 5, 0

sg.theme("DarkBlue")
sg.set_options(font=('Courier New', 16))

layout = [
    [sg.Column(column_layout(i), visible=(i==0), key=f'COLUMN {i}') for i in range(total)],
    [sg.VPush()],
    [sg.Button('< PREV'), sg.Push(), sg.Button('Exit'), sg.Push(), sg.Button('NEXT >')],
]

window = sg.Window('Title', layout, finalize=True)
window.maximize()

while True:

    event, values = window.read()
    if event==sg.WINDOW_CLOSED or event.startswith('Exit'):
        break
    elif event in ("< PREV", "NEXT >"):
        now = swap(now, 1 if event == "NEXT >" else -1)

window.close()

使用sg.TabGroup

import PySimpleGUI as sg

def tab_layout(index):
    layout = [[sg.Text(f'Window {index+1}')]]
    return layout

sg.theme("DarkBlue")
sg.set_options(font=('Courier New', 16))

total, now = 5, 0

tab_group = [[sg.Tab(f"TAB {i}", tab_layout(i), key=f"TAB {i}")] for i in range(5)]
layout = [
    [sg.TabGroup(tab_group, border_width=0, pad=(0, 0), expand_x=True, expand_y=True, key='TABGROUP')],
    [sg.VPush()],
    [sg.Button('< PREV'), sg.Push(), sg.Button('Exit'), sg.Push(), sg.Button('NEXT >')],
]
window = sg.Window("Title", layout, finalize=True)
style = sg.ttk.Style()
style.layout('TNotebook.Tab', [])                           # Hide tab bar
window.maximize()

while True:

    event, values = window.read()
    if event in (sg.WINDOW_CLOSED, "Exit"):
        break
    elif event in ("< PREV", "NEXT >"):
        now = (now+1)%total if event == "NEXT >" else (now-1)%total
        window[f"TAB {now}"].select()

window.close()