Python Forum

Full Version: How to reuse a window layout based on mysql rowcount?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good morning,
Please excuse my ignorance I have tried searching the forums to no avail.

I'm trying to generate multiple windows using pysimplegui based on a mysql select statement

There are two rows, what im struggling with is the window generation, im not sure how to implement a new window re using a window layout.

This is my test code
def resultslayout():

    mycursor = mydb.cursor()
    mycursor.execute("SELECT * FROM testtable")
    myresult = mycursor.fetchall()
    count = mycursor.rowcount
    for row in myresult:
        layout2 = [[sg.Text(row[1]+" Rowcount = " + str(count))],
                   [sg.OK()],
                   ]
    window = sg.Window('Second Form', layout2)
    event, values = window.read()
    window.close()
This generates two windows, but the same mysql result, I'm guessing i need to move the window layout out of the for loop and replace it with the window generation BUT this causes an error in regards to reusing old window layouts.

What is the correct way?

I would like a new window generated based on the row count.

Thanks for your time.
You cannot reuse a layout. This is one reason why all PySimpleGUI code has layouts defined inside of functions instead of global variables. Each time you call the function you get a new layout. But your code creates two layouts. Only one window, but two layouts. I removed the database calls and this code makes one window.
import PySimpleGUI as sg

def resultslayout():
    for word in ['Hello', 'World']:
        layout2 = [[sg.Text(word)], [sg.OK()]]
    window = sg.Window('Second Form', layout2)
    event, values = window.read()
    window.close()

resultslayout()
This code makes two windows, but you see them sequentially. The World window appears after you close the Hello window.
import PySimpleGUI as sg

def resultslayout():
    for word in ['Hello', 'World']:
        layout2 = [[sg.Text(word)], [sg.OK()]]
        window = sg.Window('Second Form', layout2)
        event, values = window.read()
        window.close()

resultslayout()
If you want to make two windows that are open at the same time, that is a bit tricky.
import PySimpleGUI as sg

def resultslayout():
    for word in ['Hello', 'World']:
        layout = [[sg.Text(word)], [sg.OK()]]
        sg.Window('Form', layout, finalize=True)

    while True:
        window, event, values = sg.read_all_windows()
        if window == sg.WIN_CLOSED:     # if all windows were closed
            break
        if event == sg.WIN_CLOSED or event == 'OK':
            window.close()

resultslayout()
This is the recipe:
https://pysimplegui.readthedocs.io/en/la...ll_windows

For multiple windows you could also create windows inside threads
https://github.com/PySimpleGUI/PySimpleG...Threads.py
Thankyou,
The above post explained it very well and has pointed me in the right direction.


Smile