0

I'm stuck on a critical part of a program I am writing. How can I repeat a layout x times and for the keys present in each layout to be different? I thought about doing a layout that has 100 already done and repeat heaps of make visible/not visible (depending on the data number available), but is there a more efficient way?

def get_selections_layouts():
    
    GSIZE = (80, 50)
    graph = sg.Graph(GSIZE, (0, 0), GSIZE, key='-GRAPH-', background_color='blue')
    graph1 = sg.Graph(GSIZE, (0, 0), GSIZE, key='-GRAPH1-', background_color='pink')
  
    spacer = sg.Column([[sg.Text('')]])
    
    layoutSelections = sg.Column([[sg.T("Data", key='dataset', justification='left'), sg.Push(),
                                   graph, graph1],
                                  [sg.HorizontalSeparator(pad=(0, 10))], ],
                                 expand_x=True)  
  
        try:
          data1 = graph.draw_text(data[1], (GSIZE[0] // 2, GSIZE[1] // 1.5), font=('Any 20', 14),
                                       text_location=sg.TEXT_LOCATION_CENTER, color='white')
       except:
          pass 
       try:            
          data2 = graph1.draw_text(data[2], (GSIZE[0] // 2, GSIZE[1] // 3), font=('Any 40', 8),
                                       text_location=sg.TEXT_LOCATION_CENTER, color='white')
        except:
            pass
    return layoutSelections


layoutSelections = get_selections_layouts()# and repeat this X times, with each layout having diff keys

layoutDataset = [[dataHeading],[start_time_text,data_total],
        layoutCol,[sg.HorizontalSeparator()],[layoutSelections]
    ]    

and in the while loop
        elif event == sg.TIMEOUT_KEY:
            currentSituation = bfo.getData
           
            try:
                data1 = currentSituation[0]#need data1 + key for layout number 1                
                window['-GRAPH-'].update(visible=True)#need -GRAPH- + key for layout number 1 
            except:
                window['-GRAPH-'].update(visible=False)
            try:
                data2 = currentSituation[0]#need data2 + key for layout number 1  
                window['-GRAPH1-'].update(visible=True)#need -GRAPH1- + key for layout number 1
            except:
                window['-GRAPH1-'].update(visible=False)

and repeat that try/except block of code for all layoutSelections with their respective keys.
4
  • Create your layout and return it by using function with argument for different key. Commented Dec 2, 2023 at 9:09
  • Use tuples as your keys. Instead of "-KEY1-"use ("-KEY-", 1). Then it's easy to "reuse" keys at least in how your code looks. The first term of the tuple stays the same and the second one is the "layout number". There are demo programs and areas of the documentation that describe this technique. Maybe search for "tuple" in them. Commented Dec 2, 2023 at 12:56
  • I see you're talking about extremely large numbers of elements (100+)... you're going to likely run into performance issues, even if they're not visible. I would have to understand the problem better... consider opening an issue on the PSG GitHub. Commented Dec 2, 2023 at 15:40
  • I've taken both advice but yes am seeing big performance issue right now. I'm probably going about it the wrong way, so will start looking at other methods still using pysimplegui. Basically it is like a table/dataframe with the row-col figures continually changing, and I am trying to display it visually by coloured squares (and text/float in the squares and space around the squares - hence looking at graph) with ability to click on each to initiate a function with appropriate args. Commented Dec 3, 2023 at 8:32

1 Answer 1

1

Create your layout and return it by using function with argument for different key.

Here's am example about how I generate same layout but with different keys for similar elements.

import PySimpleGUI as sg

def Text(text, size, justification, expand_x=None, key=None):
    return sg.Text(text, size=size, pad=(1, 1), expand_x=expand_x,
        justification=justification, key=key)

def line_frame(index, number, description, amount):
    font1 = ("Courier New", 16)
    frame_layout = [
        [Text("Product #", 10, 'c'), Text(number, 12, 'l', key=('t', index)),
         Text("Amount",     7, 'c'), Text(amount, 10, 'r'),],
        [Text(description, (39, 3), 'l', expand_x=True)],
        [sg.ProgressBar(100, orientation='h', size=(20, 10), expand_x=True, key=('p', index))],
    ]
    layout = [
        [sg.Checkbox('Print', font=font1, enable_events=True, key=f'Print {index}'),
         sg.Frame('', frame_layout, pad=((0, 5), (0, 5)), relief=sg.RELIEF_FLAT, background_color='#4b5a69'),],
    ]
    return sg.Frame(f'Line {index+1:0>4d}', layout, pad=((0, 5), 0))

data = [
    ('REQ0011118', 'USB', 1),
    ('REQ0011932', 'Tire Disposal '*5, 2),
    ('REQ0011476', 'Labor', 4.5),
    ('4500236642', 'None', 'None' ),
    ('4500221028', 'Toner', 1),
    ('4500253427', 'None', 'None'),
    ('REQ0013911', 'HP 35A BLACK TONER '*4, 30),
    ('12-64006.01', 'Radiation Oncology '*3, 1),
    ('REQ0014515', 'Black Toner Cartridge for CLJ 4700 '*5, 16),
    ('4500200308', '1" ss 90* elbow , threaded', 4),
]

font = ("Courier New", 11)
sg.theme("DarkBlue3")
sg.set_options(font=font)

column_layout = [
    [line_frame(i, number, description, amount)]
        for i, (number, description, amount) in enumerate(data)
]
layout = [
    [sg.Column(column_layout, scrollable=True, vertical_scroll_only=True, size_subsample_height=4)],
]
window = sg.Window("test", layout, margins=(0, 0), finalize=True)
for i, value in enumerate([v for v in range(10, 101, 10)]):
    window[('p', i)].update(value)

while True:
    event, values = window.read()
    if event in (sg.WINDOW_CLOSED, 'Exit'):
        break
    print(event, values)
window.close()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.