-1

I'm retrieving a large list of people from a database, then looping through each person to get their note/s and phone number/s. then displaying one person at a time with PySimpleGUI.

My issue is that it I'm doing multiple sql queries rather than just one (I don't know if this is an issue), and I have to repeatedly Finalize() and close() the screen, which creates a new window each time, rather than just refreshing the current page.

Am I able to refactor this so I don't have to close and create a new window each time?

Thank you.

# get all people
people = conn.execute('''SELECT * FROM people''')

people_tuple = people.fetchall()

# loop through all people
for index, person in enumerate(people_tuple):

    # get persons notes
    notes = conn.execute('''SELECT * FROM notes WHERE person_id = ?''', (person[0],))
    notes_list = events.fetchall()

    # get persons phone number/s
    phone_numbers = conn.execute('''SELECT * FROM phone_numbers WHERE person_id = ?''', (person[0],))

    # redacted #

    window = main_window_layout(person, phone_num_list, notes_list).Finalize()

    while True:
        event, values = window.read()

        # many if statements checking user input #

    window.close()

def main_window_layout(person, phone_num_list, notes_list):
    top_left_frame_1 = [[sg.Text("ID: " + str(person[0]), key='id', pad=(2,4))],
    [sg.Text("Name: " + person[1] + " " + person[2], key='name', pad=(2,4))]]

    frame_2 = [
    [sg.Text(note[4], key='note_date', pad=(3, (12, 3))), sg.Text(note[6], key='note_info', pad=(3, (12, 3)))]
        for note in reversed(notes_list)
    ]
horse
  • 479
  • 7
  • 25

1 Answers1

0

Try to create the layout of the window, all elements with key to update the content later.

Demo code

import PySimpleGUI as sg

person = [
    [1, 'Ronald',  'Reagan'],
    [2, 'Abraham', 'Lincoln'],
    [3, 'George',  'Washington'],
    [4, 'Andrew',  'Jackson'],
    [5, 'Thomas',  'Jefferson'],
    [6, 'Harry',   'Truman'],
]

size, index = 10, 0
total = len(person)
keys = ['ID', 'First Name', 'Last Name']
layout = [
    [sg.Text(key),
     sg.Push(),
     sg.Text(str(person[index][i]), size=size, background_color='blue', key=key)]
        for i, key in enumerate(keys)] + [
    #[sg.Button('< Prev'), sg.Push(), sg.Button('Next >')],
]
window = sg.Window('Title', layout)

while True:

    event, values = window.read(timeout=500)

    if event == sg.WIN_CLOSED:
        break

    elif event == sg.TIMEOUT_EVENT:
        index = (index + 1) % total
        for i, key in enumerate(keys):
            window[key].update(value=str(person[index][i]))

    """ button events to show previous/next record
    elif event in ('< Prev', 'Next >'):
        delta = -1 if event == '< Prev' else 1
        index = (index + delta) % total
        for i, key in enumerate(keys):
            window[key].update(value=str(person[index][i]))
    """
window.close()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23