0

I'm new to using PySimpleGUI so bear with me. I wrote a script which randomly populates 4 rows of information, on the click of a button 'Generate'. This works fine and I can click as often as I want, and every time it successfully re-generates the random for rows of string in the GUI. Quit also works like a charm. However, I'm running into issues with the 'Change CSV Path' button.

The initial click of this button opens up a second GUI window, which asks users for a csv file path to replace the data set, and this works fine. You can close and such, and Submit with no issues. No matter what you do in the CSV GUI, once the button has been clicked, it will not re-enable. You can click it a million times and it wont trigger again. Also, it doesn't matter what you did on the CSV GUI, the results are the same on the main GUI: the button 'Change CSV Path' doesn't work again.

I would like to know if this is something simple with using the while function within this, or if it's something else I'm missing!

Here is the code:

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 18 15:16:48 2021

@author: nikita twaalfhoven
"""
try:
    import PySimpleGUI as ui
except:
    print("The GUI is not installed. Run this in your Python command line:")
    print('pip install pysimplegui')
    
import listScripts as lists
import listObjects as objs

## ty 
## https://pypi.org/project/PySimpleGUI/
## https://pysimplegui.readthedocs.io/en/latest/
## https://www.geeksforgeeks.org/themes-in-pysimplegui/

try:
    # build the lists
    lists.userChosenListsCSV('D:\Dropbox\Dropbox\Business\Lee Kim\WhoWhatWhy','sampleLee.csv')
    first = objs.Who
    second = objs.What
    third = objs.Why
    fourth = objs.Cons
    
    # Set theme for UI
    ui.theme('TealMono')
    # ui.theme_previewer() 
    
    # Define the main pop-up layout
    layout = [  [ui.Text("Welcome to WhoWhatWhy MVP!")],
                [ui.Text("Hit the 'Generate' button below to play.")],
                [ui.Text("")],
                [ui.Text("Who:         "), ui.Text(size=(50,1), key ='-WHO-')],
                [ui.Text("What:        "), ui.Text(size=(50,1), key ='-WHAT-')],
                [ui.Text("Why:         "), ui.Text(size=(50,1), key ='-WHY-')],
                [ui.Text("Constraint: "), ui.Text(size=(50,1), key ='-CONS-')],
                [ui.Text("")],
                [ui.Button('Generate'), ui.Button('Change CSV Dataset'), ui.Button('Quit')]]
    
    # Create the main pop-up
    window = ui.Window("WhoWhatWhy MVP - Nikita Twaalfhoven", layout, resizable = True)
    
    
    # Define the CSV pop-up
    layoutCSV = [   [ui.Text("Add your CSV file path below, then hit 'Submit'")],
                    [ui.Text("Sample: 'C:\FolderName\sampleCSV.csv'")],
                    [ui.Input(key = '-INPUT-')],
                    [ui.Text("")],
                    [ui.Button('Submit')]]
    
    # Create the CSV pop-up
    windowCSV = ui.Window("New CSV - WhoWhatWhy MVP", layoutCSV, resizable = True)
    
    
    # The events loop
    while True:
        event, values = window.read()
        window.bring_to_front()
        
        # If user wants to Quit
        if event == ui.WIN_CLOSED or event == 'Quit':
            break
        
        # When Generate is hit
        if event == 'Generate':
            window['-WHO-'].update(first.getAnother(first))
            window['-WHAT-'].update(second.getAnother(second))
            window['-WHY-'].update(third.getAnother(third))
            window['-CONS-'].update(fourth.getAnother(fourth))
        
        # When the button is pressed for CSV
        if event == 'Change CSV Dataset':
            while True:
                event, values = windowCSV.read()
                window.bring_to_front()
                
                # If user wants to Quit
                if event == ui.WIN_CLOSED:
                    break
                
                if event == 'Submit':
                    path = values['-INPUT-']
    
                    if len(path) == 0:
                        ui.Popup("Please add a CSV path and then hit Submit. " +
                                 "Otherwise, close the CSV dialog.", 
                                 title="Empty Path")
                        
                    elif len(path) > 6 and path[len(path)-4] == ".":
                        lists.userChosenListsCSV(path)
                        first = objs.Who
                        second = objs.What
                        third = objs.Why
                        fourth = objs.Cons
        
                        window['-WHO-'].update(first.getAnother(first))
                        window['-WHAT-'].update(second.getAnother(second))
                        window['-WHY-'].update(third.getAnother(third))
                        window['-CONS-'].update(fourth.getAnother(fourth))
                        ui.Popup("CSV imported!", title="Success")
                        break
                    else:
                        ui.Popup("The CSV couldn't be updated.", title="Error")
                        break
            windowCSV.close()
    
    # Remove when done
    window.close()
    
except:
    ui.Popup("An error occured: " + str(ValueError))


EDIT: for Torben
I tried adding the windowCSV = .. line into the first loop, and this error I received: [![Error after moving 'windowCSV = ..' into the loop][1]][1]

-------------------------------------------------------------------------
EDIT: I've tried making the layout a function, and the issue persists non the less, see adjusted script below:
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 18 15:16:48 2021

@author: nikita twaalfhoven
"""
try:
    import PySimpleGUI as ui
except:
    print("The GUI is not installed. Run this in your Python command line:")
    print('pip install pysimplegui')
    
import listScripts as lists
import listObjects as objs

## ty 
## https://pypi.org/project/PySimpleGUI/
## https://pysimplegui.readthedocs.io/en/latest/
## https://www.geeksforgeeks.org/themes-in-pysimplegui/

def createWindow(ui,title,layout):
    # To create a window for the GUI
    
    return ui.Window(title,layout, resizable = True)
    
def layoutMain(ui):
    # Define the main pop-up layout
    return [  [ui.Text("Welcome to WhoWhatWhy MVP!")],
                [ui.Text("Hit the 'Generate' button below to play.")],
                [ui.Text("")],
                [ui.Text("Who:         "), ui.Text(size=(50,1), key ='-WHO-')],
                [ui.Text("What:        "), ui.Text(size=(50,1), key ='-WHAT-')],
                [ui.Text("Why:         "), ui.Text(size=(50,1), key ='-WHY-')],
                [ui.Text("Constraint: "), ui.Text(size=(50,1), key ='-CONS-')],
                [ui.Text("")],
                [ui.Button('Generate'), ui.Button('Change CSV Dataset'), ui.Button('Quit')]]
    
def layoutCSV(ui):
    # Define the CSV pop-up
    return [   [ui.Text("Add your CSV file path below, then hit 'Submit'")],
                    [ui.Text("Sample: 'C:\FolderName\sampleCSV.csv'")],
                    [ui.Input(key = '-INPUT-')],
                    [ui.Text("")],
                    [ui.Button('Submit')]]

try:
    # build the lists
    lists.userChosenListsCSV('D:\Dropbox\Dropbox\Business\Lee Kim\WhoWhatWhy','sampleLee.csv')
    first = objs.Who
    second = objs.What
    third = objs.Why
    fourth = objs.Cons
    
    # Set theme for UI
    ui.theme('TealMono')
    # ui.theme_previewer() 
    
    
    # Create the main pop-up
    window = createWindow(ui,"WhoWhatWhy MVP - Nikita Twaalfhoven", layoutMain(ui))

    
    # Create the CSV pop-up
    windowCSV = createWindow(ui,"New CSV - WhoWhatWhy MVP", layoutCSV(ui))
    
    
    # The events loop
    while True:
        event, values = window.read()
        window.bring_to_front()
        
        # If user wants to Quit
        if event == ui.WIN_CLOSED or event == 'Quit':
            break
        
        # When Generate is hit
        if event == 'Generate':
            window['-WHO-'].update(first.getAnother(first))
            window['-WHAT-'].update(second.getAnother(second))
            window['-WHY-'].update(third.getAnother(third))
            window['-CONS-'].update(fourth.getAnother(fourth))
        
        # When the button is pressed for CSV
        if event == 'Change CSV Dataset':
            
            while True:
                event, values = windowCSV.read()
                windowCSV.bring_to_front()
                
                # If user wants to Quit
                if event == ui.WIN_CLOSED:
                    break
                
                if event == 'Submit':
                    path = values['-INPUT-']
    
                    if len(path) == 0:
                        ui.Popup("Please add a CSV path and then hit Submit. " +
                                 "Otherwise, close the CSV dialog.", 
                                 title="Empty Path")
                        
                    elif len(path) > 6 and path[len(path)-4] == ".":
                        lists.userChosenListsCSV(path)
                        first = objs.Who
                        second = objs.What
                        third = objs.Why
                        fourth = objs.Cons
        
                        window['-WHO-'].update(first.getAnother(first))
                        window['-WHAT-'].update(second.getAnother(second))
                        window['-WHY-'].update(third.getAnother(third))
                        window['-CONS-'].update(fourth.getAnother(fourth))
                        ui.Popup("CSV imported!", title="Success")
                        break
                    else:
                        ui.Popup("The CSV couldn't be updated.", title="Error")
                        break
            windowCSV.close()
    
    # Remove when done
    window.close()
    
except:
    ui.Popup("An error occured: " + str(ValueError))
2
  • I would guess that you need to move the windowCSV = ... line into the loop. After you close() it the first time, it is gone and needs to be created anew. Commented Jan 19, 2021 at 5:57
  • Hey @TorbenKlein, See edit to the question, I tried adding it into the loop and it generated a PySimpleGUI error, which states the window must be used 'clean', so I can't loop it. Commented Jan 19, 2021 at 18:23

2 Answers 2

2

After first time Change CSV Dataset event generated, you do something, and after that you close the window windowCSV. So next time, no window windowCSV shown.

It's better to create function and return the window, so you need to call that function to generate the window again.

It's important, not to use the layout again. It means elements should called again, not just to use the variable layout for generation of a new window.

simple example for it

layout = [[sg.Button('OK')]]
window = sg.Window('Title', layout) # <--- OK
window.close()
window = sg.Window('Title', layout) # <--- NG, reuse layout
window.close()
layout = [[sg.Button('OK')]]
window = sg.Window('Title', layout) # <--- OK
window.close()
layout = [[sg.Button('OK')]]
window = sg.Window('Title', layout) # <--- OK
window.close()
def layout():
    return [[sg.Button('OK')]]

window = sg.Window('Title', layout())   # <--- OK
window.close()
window = sg.Window('Title', layout())   # <--- OK
window.close()

So your code updated as following
"""
layoutCSV = [   [ui.Text("Add your CSV file path below, then hit 'Submit'")],
                [ui.Text("Sample: 'C:\FolderName\sampleCSV.csv'")],
                [ui.Input(key = '-INPUT-')],
                [ui.Text("")],
                [ui.Button('Submit')]]

windowCSV = ui.Window("New CSV - WhoWhatWhy MVP", layoutCSV, resizable = True)
"""
def new_CSV_window():
    layoutCSV = [
        [ui.Text("Add your CSV file path below, then hit 'Submit'")],
        [ui.Text("Sample: 'C:\FolderName\sampleCSV.csv'")],
        [ui.Input(key = '-INPUT-')],
        [ui.Text("")],
        [ui.Button('Submit')]
    ]
    return ui.Window("New CSV - WhoWhatWhy MVP", layoutCSV, resizable = True)
"""
if event == 'Change CSV Dataset':
    while True:
"""
if event == 'Change CSV Dataset':
    windowCSV = new_CSV_window_window()
    while True:
Sign up to request clarification or add additional context in comments.

4 Comments

Hey Jason, thanks for your response. With "not use the layout again", do you mean I shouldn't call to layoutCSV a second time after I'd done it once?
window = sg.Window('Title', layout), here layout should be regenerated, not layout with old value.
Updated as above.
Thanks for the update! I tried what you suggested (see edit I'm about to add to the question), and it still doesn't work. Any other ideas?
1

Thank you Jason Yang & Torben Klein, the issue was answered when I combined both of your answers:

I defined the layouts within a function (per Jason's answer), and then moved the call to the window creation into the while loop (per Torben's suggestion). This now works! The code which runs is as follows:

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 18 15:16:48 2021

@author: nikita twaalfhoven
"""
try:
    import PySimpleGUI as ui
except:
    print("The GUI is not installed. Run this in your Python command line:")
    print('pip install pysimplegui')
    
import listScripts as lists
import listObjects as objs

## ty 
## https://pypi.org/project/PySimpleGUI/
## https://pysimplegui.readthedocs.io/en/latest/
## https://www.geeksforgeeks.org/themes-in-pysimplegui/

def createWindow(ui,title,layout):
    # To create a window for the GUI
    
    return ui.Window(title,layout, resizable = True)
    
def layoutMain(ui):
    # Define the main pop-up layout
    return [  [ui.Text("Welcome to WhoWhatWhy MVP!")],
                [ui.Text("Hit the 'Generate' button below to play.")],
                [ui.Text("")],
                [ui.Text("Who:         "), ui.Text(size=(50,1), key ='-WHO-')],
                [ui.Text("What:        "), ui.Text(size=(50,1), key ='-WHAT-')],
                [ui.Text("Why:         "), ui.Text(size=(50,1), key ='-WHY-')],
                [ui.Text("Constraint: "), ui.Text(size=(50,1), key ='-CONS-')],
                [ui.Text("")],
                [ui.Button('Generate'), ui.Button('Change CSV Dataset'), ui.Button('Quit')]]
    
def layoutCSV(ui):
    # Define the CSV pop-up
    return [   [ui.Text("Add your CSV file path below, then hit 'Submit'")],
                    [ui.Text("Sample: 'C:\FolderName\sampleCSV.csv'")],
                    [ui.Input(key = '-INPUT-')],
                    [ui.Text("")],
                    [ui.Button('Submit')]]

try:
    # build the lists
    lists.userChosenListsCSV('D:\Dropbox\Dropbox\Business\Lee Kim\WhoWhatWhy','sampleLee.csv')
    first = objs.Who
    second = objs.What
    third = objs.Why
    fourth = objs.Cons
    
    # Set theme for UI
    ui.theme('TealMono')
    # ui.theme_previewer() 
    
    
    # Create the main pop-up
    window = createWindow(ui,"WhoWhatWhy MVP - Nikita Twaalfhoven", layoutMain(ui))

    # The events loop
    while True:
        event, values = window.read()
        window.bring_to_front()
        
        # If user wants to Quit
        if event == ui.WIN_CLOSED or event == 'Quit':
            break
        
        # When Generate is hit
        if event == 'Generate':
            window['-WHO-'].update(first.getAnother(first))
            window['-WHAT-'].update(second.getAnother(second))
            window['-WHY-'].update(third.getAnother(third))
            window['-CONS-'].update(fourth.getAnother(fourth))
        
        # When the button is pressed for CSV
        if event == 'Change CSV Dataset':
            
            # Create the CSV pop-up
            windowCSV = createWindow(ui,"New CSV - WhoWhatWhy MVP", layoutCSV(ui))
            
            while True:
                event, values = windowCSV.read()
                windowCSV.bring_to_front()
                
                # If user wants to Quit
                if event == ui.WIN_CLOSED:
                    break
                
                if event == 'Submit':
                    path = values['-INPUT-']
    
                    if len(path) == 0:
                        ui.Popup("Please add a CSV path and then hit Submit. " +
                                 "Otherwise, close the CSV dialog.", 
                                 title="Empty Path")
                        
                    elif len(path) > 6 and path[len(path)-4] == ".":
                        lists.userChosenListsCSV(path)
                        first = objs.Who
                        second = objs.What
                        third = objs.Why
                        fourth = objs.Cons
        
                        window['-WHO-'].update(first.getAnother(first))
                        window['-WHAT-'].update(second.getAnother(second))
                        window['-WHY-'].update(third.getAnother(third))
                        window['-CONS-'].update(fourth.getAnother(fourth))
                        ui.Popup("CSV imported!", title="Success")
                        break
                    else:
                        ui.Popup("The CSV couldn't be updated.", title="Error")
                        break
            windowCSV.close()
    
    # Remove when done
    window.close()
    
except:
    ui.Popup("An error occured: " + str(ValueError))

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.