-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
166 lines (130 loc) · 5.33 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import os
import sys
import threading
import time
import tkinter as tk
import tkinter as ttk
from tkinter import Label, messagebox, Button
from PIL import Image, ImageTk, ImageSequence
from dotenv import load_dotenv
load_dotenv()
POPUP_DURATION = int(os.getenv("POPUP_DURATION", 60)) # Ensure these are integers
POPUP_INTERVAL = int(os.getenv("POPUP_INTERVAL", 60)) # Ensure these are integers
def resource_path(relative_path):
"""Get absolute path to resource, works for dev and for PyInstaller"""
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
class RstEyeApp:
def __init__(self, image_path, interval=POPUP_INTERVAL * 60, fullscreen=False):
self.image_path = resource_path(image_path)
self.interval = interval
self.fullscreen = fullscreen
self.root = tk.Tk()
self.root.withdraw()
if not os.path.exists(self.image_path):
messagebox.showerror("Error", f"Image file '{self.image_path}' not found.")
self.root.destroy()
return
def show_image(self):
try:
popup = tk.Toplevel(self.root)
popup.title("Please Wait")
# Set the popup window size and position it in the center
popup_width = 350
popup_height = 200
screen_width = popup.winfo_screenwidth()
screen_height = popup.winfo_screenheight()
x = (screen_width / 2) - (popup_width / 2)
y = (screen_height / 2) - (popup_height / 2)
popup.geometry(f"{popup_width}x{popup_height}+{int(x)}+{int(y)}")
# Customize the popup window with a background image and better colors
background_image = ImageTk.PhotoImage(
Image.open(resource_path("rsteye.png")).resize(
(popup_width, popup_height)
)
)
background_label = tk.Label(popup, image=background_image)
background_label.place(relwidth=1, relheight=1)
popup_label = Label(
popup,
text=f"An image is going to load in a few seconds... \n with breathing exercise for {POPUP_DURATION // 60} min\nPlease wait.",
font=("Helvetica", 12, "bold"),
wraplength=300,
)
popup_label.pack(pady=20)
# Add buttons to the popup window
button_frame = tk.Frame(popup)
button_frame.pack(pady=10)
def load_image():
# Create the main window for the GIF
window = tk.Toplevel(self.root)
window.withdraw()
window.title("Take a Break")
if self.fullscreen:
window.attributes("-fullscreen", True)
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
img = Image.open(self.image_path)
frames = [
ImageTk.PhotoImage(
frame.copy().resize(
(screen_width, screen_height), Image.LANCZOS
)
)
for frame in ImageSequence.Iterator(img)
]
label = Label(window)
label.pack()
def update_frame(frame_index):
frame = frames[frame_index]
label.configure(image=frame)
frame_index = (frame_index + 1) % len(frames)
window.after(50, update_frame, frame_index)
window.after(0, update_frame, 0)
window.deiconify()
# Close the popup window after the GIF window is displayed
popup.destroy()
window.after(POPUP_DURATION * 1000, window.destroy)
# Define button actions
def on_accept():
self.root.after(0, load_image)
def on_exit():
popup.destroy()
# Create buttons without specifying foreground or background colors
button_frame = tk.Frame(popup)
button_frame.pack(pady=10)
load_button = Button(
button_frame,
text="Yes",
command=on_accept,
font=("Helvetica", 12, "bold"),
relief="flat",
)
load_button.pack(side="left", padx=20)
exit_button = Button(
button_frame,
text="No",
command=on_exit,
font=("Helvetica", 12, "bold"),
relief="flat",
)
exit_button.pack(side="right", padx=20)
popup.mainloop()
except Exception as e:
messagebox.showerror("Error", f"Failed to load image: {e}")
def start_popup(self):
while True:
time.sleep(self.interval)
self.root.after(0, self.show_image)
def start(self):
self.root.after(0, self.show_image)
threading.Thread(target=self.start_popup, daemon=True).start()
self.root.mainloop()
if __name__ == "__main__":
image_path = "med.gif"
app = RstEyeApp(image_path, fullscreen=True)
app.start()