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))
windowCSV = ...line into the loop. After youclose()it the first time, it is gone and needs to be created anew.