generated from brennen/python_project
skeleton photobooth app added
This commit is contained in:
95
iwii_photobooth.py
Normal file
95
iwii_photobooth.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import mimetypes
|
||||
import traceback
|
||||
|
||||
from collections.abc import Callable
|
||||
import tkinter as tk
|
||||
import tkinter.messagebox as messagebox
|
||||
import tkinter.filedialog as filedialog
|
||||
import tkinter.ttk as ttk
|
||||
|
||||
from functools import wraps
|
||||
from pathlib import Path
|
||||
from platform import system
|
||||
|
||||
from serial import Serial
|
||||
|
||||
if not mimetypes.inited:
|
||||
mimetypes.init()
|
||||
|
||||
|
||||
def handle_action_exceptions(func: Callable):
|
||||
@wraps(func)
|
||||
def wrapper(event: tk.Event):
|
||||
try:
|
||||
func(event)
|
||||
except NotImplementedError as e:
|
||||
messagebox.showwarning(
|
||||
title="Warning",
|
||||
message=f"{func.__name__} Not Implemented",
|
||||
parent=event.widget.master
|
||||
)
|
||||
except Exception as e:
|
||||
messagebox.showerror(
|
||||
title="Error",
|
||||
message=str(e),
|
||||
detail=traceback.format_exc(),
|
||||
parent=event.widget.master
|
||||
)
|
||||
|
||||
event.widget.master.quit()
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
@handle_action_exceptions
|
||||
def open_preview(event: tk.Event):
|
||||
globals()["ev"] = event
|
||||
file=filedialog.askopenfilename(
|
||||
#parent=event.widget.master,
|
||||
title="Open Image",
|
||||
initialdir=Path.home(),
|
||||
filetypes=[
|
||||
(mimetype, "*"+ext) for ext, mimetype in mimetypes.types_map.items()
|
||||
if ext in (".png",".gif", ".pgm", ".ppm")
|
||||
]
|
||||
)
|
||||
|
||||
if Path(file).exists():
|
||||
# don't GC the photo when this function exits
|
||||
global _photo
|
||||
_photo=tk.PhotoImage(file=file)
|
||||
event.widget.create_image(0,0, anchor=tk.NW, image=_photo)
|
||||
|
||||
|
||||
@handle_action_exceptions
|
||||
def send_to_iwii(event: tk.Event):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def photobooth():
|
||||
app = tk.Tk()
|
||||
app.title("PhotoBooth")
|
||||
style = ttk.Style()
|
||||
|
||||
match system():
|
||||
case "Windows":
|
||||
try:
|
||||
style.theme_use("winnative")
|
||||
except tk.TclError:
|
||||
style.theme_use("vista")
|
||||
case "Darwin":
|
||||
style.theme_use("aqua")
|
||||
case "Linux":
|
||||
style.theme_use("alt")
|
||||
case _:
|
||||
style.theme_use("default")
|
||||
|
||||
preview = tk.Canvas(master=app, height=576, width=720)
|
||||
preview.bind("<space>", open_preview)
|
||||
preview.bind("<return>", send_to_iwii)
|
||||
preview.focus_set()
|
||||
preview.pack(side="top")
|
||||
app.mainloop()
|
||||
|
||||
if __name__ == "__main__":
|
||||
photobooth()
|
||||
Reference in New Issue
Block a user