I have a tier list maker program that asks for a folder and provides a list of image files in said folder to choose from. When an image file is selected, a pop up window appears to ask the user in which tier(S, A, B, C, D, F) they would like to put the image in. However aside from the S-tier row, you have to click on the button multiple times to perform the action. Here's the code.
`#Image_Viewer.py
import PySimpleGUI as sg
import os.path
import io
from PIL import Image
#Changed the theme of the python programme because why not
sg.theme('DarkAmber')
#Folder selector column (Left side of the program)
image_list_column = [
[sg.Text('Image Folder'),
sg.Input(size=(25,1), enable_events=True, key='-FOLDER INPUT-'),
sg.FolderBrowse()],
[sg.Text('Only ".PNG" and ".GIF" files can be used.')],
[sg.Listbox(values=[], enable_events=True, size=(40, 20),key=('-IMAGE LIST-'))],
]
# Here are the rows for the program
image_row_column = [
[sg.Image(key='-STIER-', size = (50, 50))],
[sg.HSep()],
[sg.Image(key='-ATIER-')],
[sg.HSep()],
[sg.Image(key='-BTIER-')],
[sg.HSep()],
[sg.Image(key='-CTIER-')],
[sg.HSep()],
[sg.Image(key='-DTIER-')],
[sg.HSep()],
[sg.Image(key='-FTIER-')],
]
#Here is the label column left of the folder selector
row_label_column = [
[sg.Canvas(background_color='gray', size=(60,480))],
]
# ----- full layout -----
layout = [
[
sg.Column(image_list_column),
sg.Column(row_label_column),
sg.VSeperator(),
sg.Column(image_row_column),
]
]
#window:)
window = sg.Window('Tier List Organizer Viewer', layout)
#event loop
while True:
event, values = window.read()
if event == 'EXIT' or event == sg.WIN_CLOSED:
break
# folder name was filled in, so make a list of files
if event == '-FOLDER INPUT-':
folder = values['-FOLDER INPUT-']
try:
# get list of files in folder
file_list = os.listdir(folder)
except:
file_list = []
fnames = [
f
for f in file_list
if os.path.isfile(os.path.join(folder, f))
and f.lower().endswith(('.png', '.gif', '.jpg'))]
window['-IMAGE LIST-'].update(fnames)
elif event == '-IMAGE LIST-': # a file was chosen by the user from the list
# Here goes the popup window for when a user clicks on an image file, the row chooser
# S Tier
if sg.Window('Row Selector', [[sg.T('Where do you want to place this image?')],
[sg.Button('S Tier',key='-S CHOICE-'), sg.Button('A Tier', key='-A CHOICE-'), sg.Button('B Tier', key='-B CHOICE-'), sg.Button('C Tier', key='-C CHOICE-'), sg.Button('D Tier', key='-D CHOICE-'), sg.Button('F Tier', key='-F CHOICE-'), sg.Cancel()]]).read(close=True)[0]== "-S CHOICE-":
file_name = Image.open(os.path.join(values['-FOLDER INPUT-'], values['-IMAGE LIST-'][0]))
file_name.thumbnail((200, 200))
bio = io.BytesIO()
file_name.save(bio, format="PNG")
window["-STIER-"].update(data=bio.getvalue())
#A Tier
elif sg.Window('Row Selector', [[sg.T('Where do you want to place this image?')],
[sg.Button('S Tier',key='-S CHOICE-'), sg.Button('A Tier', key='-A CHOICE-'), sg.Button('B Tier', key='-B CHOICE-'), sg.Button('C Tier', key='-C CHOICE-'), sg.Button('D Tier', key='-D CHOICE-'), sg.Button('F Tier', key='-F CHOICE-'), sg.Cancel()]]).read(close=True)[0]== "-A CHOICE-":
file_name = Image.open(os.path.join(values['-FOLDER INPUT-'], values['-IMAGE LIST-'][0]))
file_name.thumbnail((200, 200))
bio = io.BytesIO()
file_name.save(bio, format="PNG")
window["-ATIER-"].update(data=bio.getvalue())
# B Tier
elif sg.Window('Row Selector', [[sg.T('Where do you want to place this image?')],
[sg.Button('S Tier',key='-S CHOICE-'), sg.Button('A Tier', key='-A CHOICE-'), sg.Button('B Tier', key='-B CHOICE-'), sg.Button('C Tier', key='-C CHOICE-'), sg.Button('D Tier', key='-D CHOICE-'), sg.Button('F Tier', key='-F CHOICE-'), sg.Cancel()]]).read(close=True)[0]== "-B CHOICE-":
file_name = Image.open(os.path.join(values['-FOLDER INPUT-'], values['-IMAGE LIST-'][0]))
file_name.thumbnail((200, 200))
bio = io.BytesIO()
file_name.save(bio, format="PNG")
window["-BTIER-"].update(data=bio.getvalue())
# C Tier
elif sg.Window('Row Selector', [[sg.T('Where do you want to place this image?')],
[sg.Button('S Tier',key='-S CHOICE-'), sg.Button('A Tier', key='-A CHOICE-'), sg.Button('B Tier', key='-B CHOICE-'), sg.Button('C Tier', key='-C CHOICE-'), sg.Button('D Tier', key='-D CHOICE-'), sg.Button('F Tier', key='-F CHOICE-'), sg.Cancel()]]).read(close=True)[0]== "-C CHOICE-":
file_name = Image.open(os.path.join(values['-FOLDER INPUT-'], values['-IMAGE LIST-'][0]))
file_name.thumbnail((200, 200))
bio = io.BytesIO()
file_name.save(bio, format="PNG")
window["-CTIER-"].update(data=bio.getvalue())
# D Tier
elif sg.Window('Row Selector', [[sg.T('Where do you want to place this image?')],
[sg.Button('S Tier',key='-S CHOICE-'), sg.Button('A Tier', key='-A CHOICE-'), sg.Button('B Tier', key='-B CHOICE-'), sg.Button('C Tier', key='-C CHOICE-'), sg.Button('D Tier', key='-D CHOICE-'), sg.Button('F Tier', key='-F CHOICE-'), sg.Cancel()]]).read(close=True)[0]== "-D CHOICE-":
file_name = Image.open(os.path.join(values['-FOLDER INPUT-'], values['-IMAGE LIST-'][0]))
file_name.thumbnail((200, 200))
bio = io.BytesIO()
file_name.save(bio, format="PNG")
window["-DTIER-"].update(data=bio.getvalue())
# F Tier
elif sg.Window('Row Selector', [[sg.T('Where do you want to place this image?')],
[sg.Button('S Tier',key='-S CHOICE-'), sg.Button('A Tier', key='-A CHOICE-'), sg.Button('B Tier', key='-B CHOICE-'), sg.Button('C Tier', key='-C CHOICE-'), sg.Button('D Tier', key='-D CHOICE-'), sg.Button('F Tier', key='-F CHOICE-'), sg.Cancel()]]).read(close=True)[0]== "-F CHOICE-":
file_name = Image.open(os.path.join(values['-FOLDER INPUT-'], values['-IMAGE LIST-'][0]))
file_name.thumbnail((200, 200))
bio = io.BytesIO()
file_name.save(bio, format="PNG")
window["-FTIER-"].update(data=bio.getvalue())
window.close()`
I've tried to use the pop up element before trying to use the window element instead. I'm trying to get it so each button for every row only has to be selected once.
CodePudding user response:
IMO, it's about the if/elif/else
statement in Python, each elif/else
will be executed if previous condition not met, that's why the user popup shown again if it is not Button "-S CHOICE-"
clicked.
For easy, you can define a function for the user popup, like
def popup():
layout = [
[sg.T('Where do you want to place this image?')]] [
[sg.Button(f'{kind} Tier', key=kind) for kind in "SABCDF"] [sg.Cancel()]
]
window = sg.Window('Row Selector', layout)
event, values = window.read()
return None if event in (sg.WIN_CLOSED, 'Cancel') else event
then part of your code will be reduced to
elif event == '-IMAGE LIST-': # a file was chosen by the user from the list
# Here goes the popup window for when a user clicks on an image file, the row chooser
kind = popup()
if kind:
file_name = Image.open(os.path.join(values['-FOLDER INPUT-'], values['-IMAGE LIST-'][0]))
file_name.thumbnail((200, 200))
bio = io.BytesIO()
file_name.save(bio, format="PNG")
window[f"-{kind}TIER-"].update(data=bio.getvalue())