Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 75c8e5c

Browse files
Update image_viewer.py
Update to the GUI - Window Elements added.
1 parent 26a8aa0 commit 75c8e5c

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

‎Sample GUI Implementation/image_viewer.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,78 @@
99
1010
extension = values["image_file"].lower().split(".")[-1]
1111
if extension in ["jpg", "jpeg"]: # JPG file
12-
new_filename = values["image_file"].replace(extension, "png")
13-
im = Image.open(values["image_file"])
14-
im.save(new_filename)
12+
new_filename = values["image_file"].replace(extension, "png")
13+
im = Image.open(values["image_file"])
14+
im.save(new_filename)
1515
'''
1616

1717
import os.path
1818
import PySimpleGUI as sg
1919

2020
FILE_SELECT_COLUMN_LAYOUT = [
21-
[sg.Text("Image Folder"),
22-
sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
23-
sg.FolderBrowse(),],
24-
[sg.Listbox(values=[], enable_events=True, size=(40, 20), key="-FILE LIST-")],]
21+
[sg.Text("Image Folder:")],
22+
[sg.In(size=(52, 1), enable_events=True, key="-FOLDER-"),
23+
sg.FolderBrowse(tooltip="Select a folder"),],
24+
[sg.Text("Images Retrieved: **(PNG/GIF)")],
25+
[sg.Listbox(values=[], enable_events=True, size=(59, 20), key="-FILE LIST-")],]
2526

2627
IMAGE_VIEWER_COLUMN_LAYOUT = [
27-
[sg.Text("Choose an image from list on left:")],
28-
[sg.Text(size=(40, 1), key="-IMAGE_FILE-")],
28+
[sg.Text(" Select an image from list", key="-STATIC_TEXT-",
29+
font=("Helvetica", 25))],
30+
[sg.Text(size=(110, 1), key="-IMAGE_FILE-")],
2931
[sg.Image(key="-IMAGE-")],
3032
]
3133

3234
# ----- Full Window Layout -----
3335
WINDOW_LAYOUT = [[sg.Column(FILE_SELECT_COLUMN_LAYOUT),
34-
sg.VSeperator(), sg.Column(IMAGE_VIEWER_COLUMN_LAYOUT),]]
36+
sg.VSeperator(), sg.Column(IMAGE_VIEWER_COLUMN_LAYOUT)],
37+
[sg.Button("Reset", key="-RESET-"), sg.Button("Exit", key="-Exit-")],]
3538

3639
CURRENT_WORKING_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
3740
WINDOW = sg.Window("Image Viewer", WINDOW_LAYOUT, icon=CURRENT_WORKING_DIRECTORY + "\\img_view.ico")
3841

3942
# Run the Event Loop
4043
while True:
4144
EVENT, VALUES = WINDOW.read()
42-
if EVENT in (sg.WIN_CLOSED, "Exit"):
45+
46+
# Exit the viewer in the event of close clicked or exit clicked.
47+
if EVENT in (sg.WIN_CLOSED, "-Exit-"):
4348
break
44-
# Folder name was filled in, make a list of files in the folder
49+
50+
# Restore the window to the initial state on the event of Reset.
51+
if EVENT == "-RESET-":
52+
WINDOW["-IMAGE_FILE-"].update('')
53+
WINDOW["-IMAGE-"].update(filename='')
54+
WINDOW['-FILE LIST-'].update(values=[])
55+
WINDOW["-STATIC_TEXT-"].update(" Select an image from list",
56+
font=("Helvetica", 25))
57+
WINDOW["-FOLDER-"].update('')
58+
59+
# Extract the list of image files in the selected folder.
4560
if EVENT == "-FOLDER-":
4661
FOLDER = VALUES["-FOLDER-"]
4762
try:
4863
# Get list of files in folder
4964
FILE_LIST = os.listdir(FOLDER)
50-
except:
65+
except:# pylint: disable=bare-except
5166
FILE_LIST = []
5267

53-
FNAMES = [
54-
imgfile for imgfile in FILE_LIST
55-
if os.path.isfile(os.path.join(FOLDER, imgfile))
56-
and imgfile.lower().endswith((".png", ".gif"))
57-
]
68+
FNAMES = [imgfile for imgfile in FILE_LIST
69+
if os.path.isfile(os.path.join(FOLDER, imgfile))
70+
and imgfile.lower().endswith((".png", ".gif"))]
71+
5872
WINDOW["-FILE LIST-"].update(FNAMES)
59-
elif EVENT == "-FILE LIST-": # A file was chosen from the listbox
73+
74+
elif EVENT == "-FILE LIST-": # Display the image selected.
6075
try:
6176
FILENAME = os.path.join(
6277
VALUES["-FOLDER-"], VALUES["-FILE LIST-"][0]
6378
)
6479
WINDOW["-IMAGE_FILE-"].update(FILENAME)
6580
WINDOW["-IMAGE-"].update(filename=FILENAME)
81+
WINDOW["-STATIC_TEXT-"].update("Image Selected:", font=("Helvetica", 12))
6682

67-
except:
83+
except:# pylint: disable=bare-except
6884
pass
6985

7086
WINDOW.close()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /