简体   繁体   中英

PySimpleGUI: How to not display empty field

I've created the simple GUI as follow. In the example only three rows are included, however the same principle can be applied to more of them. I would like to fill all or same rows in the GUI and the save it, but most importantly when the saved files is loaded back only the filled rows (and not all) should be displayed. I've tried in the way below but unsuccessfully..

import PySimpleGUI as sg
from datetime import datetime
import base64
from pathlib import Path
import webbrowser

sg.theme('DarkTeal9')      


layout_1 = [[sg.InputText("", key="-IT2-", font='Arial 9', size=(10,1)),
             sg.Combo(["Item1", "Item2", "Item3"],size=(20,1), key='-TEST2-', font='Arial 9'),
             sg.CalendarButton("", close_when_date_chosen=True,  target='-IN2-', font='Arial 9', no_titlebar=False, format='%d-%b-%Y'),
             sg.InputText("", key='-IN2-', size=(20,1), font='Arial 9')]]


layout_a = [[sg.Button("row 2")]]

layout_2 = [[sg.InputText("", key="-IT3-", font='Arial 9', size=(10,1)),
             sg.Combo(["Item1", "Item2", "Item3"],size=(20,1), key='-TEST3-', font='Arial 9'),
             sg.CalendarButton("", close_when_date_chosen=True,  target='-IN3-', font='Arial 9', no_titlebar=False, format='%d-%b-%Y'),
             sg.InputText("", key='-IN3-', size=(20,1), font='Arial 9')]]

layout_b =[[sg.Button("row 3")]]

layout_3 = [[sg.InputText("", key="-IT4-", font='Arial 9', size=(10,1), visible=True),
             sg.Combo(["Item1", "Item2", "Item3"],size=(20,1), key='-TEST4-', font='Arial 9'),
             sg.CalendarButton("", close_when_date_chosen=True,  target='-IN4-', font='Arial 9', no_titlebar=False, format='%d-%b-%Y'),
             sg.InputText("", key='-IN4-', size=(20,1), font='Arial 9', justification="c")]]

               
layout = [
          [sg.Column(layout_1, key='-LAY1-'), sg.Column(layout_a, visible=True, key="-LAYA-")],
          [sg.Column(layout_2, visible=False, key='-LAY2-'), sg.Column(layout_b, visible=False, key='-LAYB-')],
          [sg.Column(layout_3, visible=False, key='-LAY3-')],
          [sg.Button ("Save"), sg.Button ("Load"), sg.Button('Exit'),],
          ]
        

window = sg.Window("", layout)


while True:
    event, values = window.read()
        
    if event == 'Save':
        file_name = sg.popup_get_file("Save", save_as=True, no_window=True)
        window.SaveToDisk(file_name)
    if event == 'Load':
        file_name = sg.popup_get_file('Load', no_window=True)
        window.LoadFromDisk(file_name)
        if values["-IT2-"] != "":
            window[f'-LAY1-'].update(visible=True)
            window[f'-LAYA-'].update(visible=False)
        if values ["-IT3-"] != "":
            window[f'-LAY2-'].update(visible=True)
        if values["-IT4-"] != "":
            window[f'-LAY3-'].update(visible=True)
            
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
        window.close()
    if event == 'row 2':
        window[f'-LAY2-'].update(visible=True)
        window[f'-LAYA-'].update(visible=False)
        window[f'-LAYB-'].update(visible=True)
        layout = str(event)
    if event == 'row 3':
        window[f'-LAY3-'].update(visible=True)
        window[f'-LAYB-'].update(visible=False)
        layout = str(event)

window.close()

Try it

    if event == 'Load':
        file_name = sg.popup_get_file('Load', no_window=True)
        window.LoadFromDisk(file_name)
        v1, v2, v3 = window["-IT2-"].get(), window["-IT3-"].get(), window["-IT4-"].get()
        if v1:
            window[f'-LAY1-'].update(visible=True)
            window[f'-LAYA-'].update(visible=False)
        if v2:
            window[f'-LAY2-'].update(visible=True)
        if v3:
            window[f'-LAY3-'].update(visible=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM