Skip to content

Commit ceafe78

Browse files
New CANVAS Demo, updated PNG Viewer Demo
1 parent e7c216d commit ceafe78

2 files changed

Lines changed: 45 additions & 15 deletions

File tree

Demo_Canvas.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import PySimpleGUI as gui
2+
3+
canvas = gui.Canvas(size=(100,100), background_color='red')
4+
5+
layout = [
6+
[canvas],
7+
[gui.T('Change circle color to:'), gui.ReadFormButton('Red'), gui.ReadFormButton('Blue')]
8+
]
9+
10+
form = gui.FlexForm('Canvas test')
11+
form.Layout(layout)
12+
form.ReadNonBlocking()
13+
14+
cir = canvas.TKCanvas.create_oval(50, 50, 100, 100)
15+
16+
while True:
17+
button, values = form.Read()
18+
if button is None: break
19+
if button is 'Blue':
20+
canvas.TKCanvas.itemconfig(cir, fill = "Blue")
21+
elif button is 'Red':
22+
canvas.TKCanvas.itemconfig(cir, fill = "Red")

Demo_PNG_Viewer.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,62 @@
44
# Simple Image Browser based on PySimpleGUI
55

66
# Get the folder containing the images from the user
7-
rc, folder = sg.GetPathBox('Image Browser', 'Image folder to open', default_path='A:/TEMP/PDFs')
7+
rc, folder = sg.GetPathBox('Image Browser', 'Image folder to open', default_path='')
88
if rc is False or folder is '':
99
sg.MsgBoxCancel('Cancelling')
1010
exit(0)
1111

1212
# get list of PNG files in folder
1313
png_files = [folder + '\\' + f for f in os.listdir(folder) if '.png' in f]
14+
filenames_only = [f for f in os.listdir(folder) if '.png' in f]
1415

1516
if len(png_files) == 0:
1617
sg.MsgBox('No PNG images in folder')
1718
exit(0)
1819

1920
# create the form that also returns keyboard events
20-
form = sg.FlexForm('Image Browser', return_keyboard_events=True)
21+
form = sg.FlexForm('Image Browser', return_keyboard_events=True, location=(0,0), use_default_focus=False )
2122

2223
# make these 2 elements outside the layout because want to "update" them later
2324
# initialize to the first PNG file in the list
2425
image_elem = sg.Image(filename=png_files[0])
2526
filename_display_elem = sg.Text(png_files[0], size=(80, 3))
26-
file_num_display_elem = sg.Text('File 1 of {}'.format(len(png_files)), size=(10,1))
27+
file_num_display_elem = sg.Text('File 1 of {}'.format(len(png_files)), size=(15,1))
2728

2829
# define layout, show and read the form
29-
layout = [[filename_display_elem],
30+
col = [[filename_display_elem],
3031
[image_elem],
3132
[sg.ReadFormButton('Next', size=(8,2)), sg.ReadFormButton('Prev', size=(8,2)), file_num_display_elem]]
3233

33-
form.LayoutAndRead(layout) # Shows form on screen
34+
col_files = [[sg.Listbox(values=filenames_only, size=(60,30), key='listbox')],
35+
[sg.ReadFormButton('Read')]]
36+
layout = [[sg.Column(col_files), sg.Column(col)]]
37+
button, values = form.LayoutAndRead(layout) # Shows form on screen
3438

3539
# loop reading the user input and displaying image, filename
3640
i=0
3741
while True:
38-
f = png_files[i]
39-
# update window with new image
40-
image_elem.Update(filename=f)
41-
# update window with filename
42-
filename_display_elem.Update(f)
43-
# update page display
44-
file_num_display_elem.Update('File {} of {}'.format(i+1, len(png_files)))
45-
# read the form
46-
button, values = form.Read()
4742

4843
# perform button and keyboard operations
4944
if button is None:
5045
break
51-
elif button in ('Next', 'MouseWheel:Down', 'Down:40', 'Next:34') and i < len(png_files):
46+
elif button in ('Next', 'MouseWheel:Down', 'Down:40', 'Next:34') and i < len(png_files)-1:
5247
i += 1
5348
elif button in ('Prev', 'MouseWheel:Up', 'Up:38', 'Prior:33') and i > 0:
5449
i -= 1
5550

51+
if button == 'Read':
52+
filename = folder + '\\' + values['listbox'][0]
53+
# print(filename)
54+
else:
55+
filename = png_files[i]
5656

57+
# update window with new image
58+
image_elem.Update(filename=filename)
59+
# update window with filename
60+
filename_display_elem.Update(filename)
61+
# update page display
62+
file_num_display_elem.Update('File {} of {}'.format(i+1, len(png_files)))
5763

64+
# read the form
65+
button, values = form.Read()

0 commit comments

Comments
 (0)