Skip to content

Commit fda0a94

Browse files
Up/Down Arrow Key selection of images.
1. Reusable functions added. 2. Up/Down selection and viewing of the images added.
1 parent 24ce46b commit fda0a94

File tree

1 file changed

+51
-19
lines changed

1 file changed

+51
-19
lines changed

Sample GUI Implementation/image_viewer.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,46 @@
2222
import PySimpleGUI as sg
2323
import cv2
2424

25+
def process_and_view_image():
26+
'''Process and display the image.'''
27+
try:
28+
file_name = os.path.join(
29+
VALUES["-FOLDER-"], VALUES["-FILE LIST-"][0]
30+
)
31+
WINDOW["-IMAGE_FILE-"].update(file_name)
32+
image = cv2.imread(file_name) # pylint: disable=no-member
33+
34+
# Resize the image if the dimension is to large.
35+
if image.shape[0] > 999 and image.shape[1] > 768:
36+
image = cv2.resize(image, (1024, 768), interpolation=cv2.INTER_NEAREST) # pylint: disable=no-member
37+
38+
WINDOW["-IMAGE-"].update(data=cv2.imencode('.png', image)[1].tobytes()) # pylint: disable=no-member
39+
# WINDOW["-IMAGE-"].update(filename=FILENAME)
40+
WINDOW["-STATIC_TEXT-"].update("Image Selected:", font=("Helvetica", 12))
41+
42+
except: # pylint: disable=bare-except
43+
pass
44+
45+
def up_down_arrow_key_selection(arrow_key_event):
46+
'''Process the list selection usingt he Up and Down arrow key events.
47+
More information related to up/down arrow key seletion on a listbox
48+
can be viewed @ https://github.com/PySimpleGUI/PySimpleGUI/issues/1629'''
49+
50+
try:
51+
current_index = WINDOW.Element('-FILE LIST-').Widget.curselection()
52+
53+
if arrow_key_event == 'Up:38':
54+
current_index = (current_index[0] - 1) % WINDOW.Element('-FILE LIST-').Widget.size()
55+
elif arrow_key_event == 'Down:40':
56+
current_index = (current_index[0] + 1) % WINDOW.Element('-FILE LIST-').Widget.size()
57+
58+
WINDOW.Element('-FILE LIST-').Update(set_to_index=current_index)
59+
WINDOW.Element('-FILE LIST-').Update(scroll_to_index=current_index)
60+
WINDOW.write_event_value('-FILE LIST-',
61+
[WINDOW.Element('-FILE LIST-').GetListValues()[current_index]])
62+
except: # pylint: disable=bare-except
63+
pass
64+
2565
sg.theme('dark grey 9')
2666

2767
FILE_SELECT_COLUMN_LAYOUT = [
@@ -30,7 +70,8 @@
3070
readonly=True, disabled_readonly_background_color='#40444B'),
3171
sg.FolderBrowse(tooltip="Select a folder", key='-FOLDER_BROWSE-'),],
3272
[sg.Text("Images Retrieved:")],
33-
[sg.Listbox(values=[], enable_events=True, size=(80, 45), key="-FILE LIST-")],
73+
[sg.Listbox(values=[], enable_events=True, size=(80, 45),
74+
key="-FILE LIST-", bind_return_key=True)],
3475
[sg.Button("Reset", key="-RESET-"), sg.Button("Exit", key="-Exit-")],]
3576

3677
IMAGE_VIEWER_COLUMN_LAYOUT = [
@@ -48,7 +89,7 @@
4889
CURRENT_WORKING_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
4990
WINDOW = sg.Window("Py Image Viewer", WINDOW_LAYOUT, margins=(0, 0),
5091
icon=CURRENT_WORKING_DIRECTORY + "\\img_view.ico",
51-
resizable=False, finalize=True)
92+
resizable=False, finalize=True, return_keyboard_events=True)
5293

5394
# Run the Event Loop
5495
while True:
@@ -82,22 +123,13 @@
82123
WINDOW["-FILE LIST-"].update(FNAMES)
83124

84125
elif EVENT == "-FILE LIST-": # Display the image selected.
85-
try:
86-
FILENAME = os.path.join(
87-
VALUES["-FOLDER-"], VALUES["-FILE LIST-"][0]
88-
)
89-
WINDOW["-IMAGE_FILE-"].update(FILENAME)
90-
IMAGE = cv2.imread(FILENAME) # pylint: disable=no-member
91-
92-
# Resize the image if the dimension is to large.
93-
if IMAGE.shape[0] > 999 and IMAGE.shape[1] > 768:
94-
IMAGE = cv2.resize(IMAGE, (1024, 768), interpolation=cv2.INTER_NEAREST) # pylint: disable=no-member
95-
96-
WINDOW["-IMAGE-"].update(data=cv2.imencode('.png', IMAGE)[1].tobytes()) # pylint: disable=no-member
97-
# WINDOW["-IMAGE-"].update(filename=FILENAME)
98-
WINDOW["-STATIC_TEXT-"].update("Image Selected:", font=("Helvetica", 12))
99-
100-
except: # pylint: disable=bare-except
101-
pass
126+
process_and_view_image()
127+
128+
elif EVENT == 'Up:38':
129+
up_down_arrow_key_selection(EVENT)
130+
process_and_view_image()
131+
elif EVENT == 'Down:40':
132+
up_down_arrow_key_selection(EVENT)
133+
process_and_view_image()
102134

103135
WINDOW.close()

0 commit comments

Comments
 (0)