Skip to content

Commit b6262b2

Browse files
Popups & Sample GUIs
Exploring PySimpleGUI
1 parent 6530540 commit b6262b2

21 files changed

+192
-31
lines changed

One_Shot_Window.py

Lines changed: 0 additions & 31 deletions
This file was deleted.
File renamed without changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''A Simple GUI Login Popup.'''
2+
3+
import PySimpleGUI as sg
4+
5+
# Define the window layout which consists of a username label,
6+
# input text box, a password label and a password textbox where the
7+
# input characters are masked.
8+
LAYOUT = [[sg.Text("Name:")],
9+
[sg.Input(key='-INPUT-')],
10+
[sg.Text('Password:')],
11+
[sg.InputText('', key='Password', password_char='*')],
12+
[sg.Button('OK'), sg.Button('Cancel')]]
13+
14+
# Create the window
15+
WINDOW = sg.Window('Login', LAYOUT)
16+
17+
# Display and interact with the Window using an Event Loop.
18+
# When a button is clicked, the click event returns the text
19+
# of the button clicked by the user.
20+
while True:
21+
EVENT, VALUES = WINDOW.read()
22+
# See if user wants to quit or window was closed
23+
if EVENT in (sg.WINDOW_CLOSED, 'Cancel'):
24+
break
25+
26+
# if the user clicks OK, display a message with login success.
27+
# No validation is added at this point as this is purely to
28+
# introduce the GUI concepts.
29+
if EVENT == 'OK':
30+
sg.Popup('SUCCESS! (This is to ensure that the message is\
31+
long enough for the popup title to be visible)',
32+
title='Login Result', modal=True)
33+
34+
# Finish up by removing from the screen
35+
WINDOW.close()
File renamed without changes.
File renamed without changes.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'''IMAGE VIEWER
2+
3+
PySimpleGUI reads images in PNG, GIF, PPM/PGM format.
4+
JPEGs cannot be shown because tkinter does not naively support these formats.
5+
JPEGs can be converted to PNG format using the Python Imaging Library (PIL)
6+
package prior to viewing them using PySimpleGUI package.
7+
8+
Sample code for conversion from JPEG/JPG to PNGs is hsown in the commented code block below.
9+
10+
extension = values["image_file"].lower().split(".")[-1]
11+
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)
15+
'''
16+
17+
import os.path
18+
import PySimpleGUI as sg
19+
20+
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-")],]
25+
26+
IMAGE_VIEWER_COLUMN_LAYOUT = [
27+
[sg.Text("Choose an image from list on left:")],
28+
[sg.Text(size=(40, 1), key="-IMAGE_FILE-")],
29+
[sg.Image(key="-IMAGE-")],
30+
]
31+
32+
# ----- Full Window Layout -----
33+
WINDOW_LAYOUT = [[sg.Column(FILE_SELECT_COLUMN_LAYOUT),
34+
sg.VSeperator(), sg.Column(IMAGE_VIEWER_COLUMN_LAYOUT),]]
35+
36+
CURRENT_WORKING_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
37+
WINDOW = sg.Window("Image Viewer", WINDOW_LAYOUT, icon=CURRENT_WORKING_DIRECTORY + "\\img_view.ico")
38+
39+
# Run the Event Loop
40+
while True:
41+
EVENT, VALUES = WINDOW.read()
42+
if EVENT in (sg.WIN_CLOSED, "Exit"):
43+
break
44+
# Folder name was filled in, make a list of files in the folder
45+
if EVENT == "-FOLDER-":
46+
FOLDER = VALUES["-FOLDER-"]
47+
try:
48+
# Get list of files in folder
49+
FILE_LIST = os.listdir(FOLDER)
50+
except:
51+
FILE_LIST = []
52+
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+
]
58+
WINDOW["-FILE LIST-"].update(FNAMES)
59+
elif EVENT == "-FILE LIST-": # A file was chosen from the listbox
60+
try:
61+
FILENAME = os.path.join(
62+
VALUES["-FOLDER-"], VALUES["-FILE LIST-"][0]
63+
)
64+
WINDOW["-IMAGE_FILE-"].update(FILENAME)
65+
WINDOW["-IMAGE-"].update(filename=FILENAME)
66+
67+
except:
68+
pass
69+
70+
WINDOW.close()
204 KB
Binary file not shown.
18.7 KB
Loading
66.1 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'''ANIMATED POPUP'''
2+
3+
import PySimpleGUI as sg
4+
5+
for i in range(100000):
6+
sg.popup_animated(sg.DEFAULT_BASE64_LOADING_GIF, title='Animated Popup',
7+
no_titlebar=False, background_color='white', time_between_frames=100)

0 commit comments

Comments
 (0)