Python Forum
[PyGUI] How to reuse a window layout based on mysql rowcount?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGUI] How to reuse a window layout based on mysql rowcount?
#1
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.
Reply
#2
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
retroisbest likes this post
Reply
#3
Thankyou,
The above post explained it very well and has pointed me in the right direction.


Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 471 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [PyQt] How to clip layout to sides and bottom of main window? Valmont 9 4,884 Mar-24-2021, 10:00 PM
Last Post: deanhystad
  pyqt5 layout Nickd12 8 3,476 Jan-18-2021, 09:09 AM
Last Post: Axel_Erfurt
  tkinter window and turtle window error 1885 3 6,698 Nov-02-2019, 12:18 PM
Last Post: 1885
  Python GUI layout off between different OS shift838 5 3,681 Jan-02-2019, 02:53 AM
Last Post: shift838
  PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary kennybassett 2 4,746 Oct-02-2018, 02:05 PM
Last Post: Alfalfa
  update a variable in parent window after closing its toplevel window gray 5 9,059 Mar-20-2017, 10:35 PM
Last Post: Larz60+
  [Tkinter] grid layout neech 8 17,584 Oct-14-2016, 07:06 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020