-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
189 lines (155 loc) · 6.71 KB
/
main.py
File metadata and controls
189 lines (155 loc) · 6.71 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import tkinter as tk
import csv, random, os
# --- Load prompts ---
def load_prompts(filename):
with open(filename, newline='', encoding='utf-8', errors='replace') as f:
return [item for row in csv.reader(f) for item in row if item.strip()]
# --- Random prompt ---
def show_random_prompt(event=None):
prompt_text = random.choice(prompts) if prompts else "No prompts found!"
canvas.itemconfig(prompt_id, text=prompt_text)
# --- Rounded rectangle function ---
def round_rectangle(x1, y1, x2, y2, r=20, **kwargs):
points = [
x1+r, y1,
x2-r, y1,
x2, y1,
x2, y1+r,
x2, y2-r,
x2, y2,
x2-r, y2,
x1+r, y2,
x1, y2,
x1, y2-r,
x1, y1+r,
x1, y1
]
return canvas.create_polygon(points, smooth=True, **kwargs)
# --- Back button creation ---
def create_back_btn(theme_color):
# Remove old button if exists
if hasattr(canvas, 'back_btn') and hasattr(canvas, 'back_text'):
canvas.delete(canvas.back_btn)
canvas.delete(canvas.back_text)
# Slightly above bottom-left
x1, y1, x2, y2 = 10, 10, 50, 40
canvas.back_btn = round_rectangle(x1, y1, x2, y2, r=10, fill=theme_color, outline="")
canvas.back_text = canvas.create_text((x1+x2)//2, (y1+y2)//2, text="◀", font=("Arial", 12, "bold"), fill="white")
canvas.tag_bind(canvas.back_btn, "<Button-1>", lambda e: back_to_start())
canvas.tag_bind(canvas.back_text, "<Button-1>", lambda e: back_to_start())
# --- Switch screens ---
def start_app(theme_color, bg_file):
# Update theme
canvas.itemconfig(prompt_id, text="Click to get a prompt", fill=theme_color)
# Remove old background image if it exists
if hasattr(canvas, 'bg_image_item'):
canvas.delete(canvas.bg_image_item)
# Load background image if it exists
if os.path.exists(bg_file):
try:
bg_image = tk.PhotoImage(file=bg_file)
canvas.bg_image = bg_image # Keep reference
canvas.bg_image_item = canvas.create_image(0, 0, image=bg_image, anchor="nw")
canvas.tag_lower(canvas.bg_image_item) # Move to background
except:
canvas.config(bg=theme_color)
else:
canvas.config(bg=theme_color)
# Remove old buttons if they exist
for attr in ['generate_btn', 'generate_text', 'close_btn', 'close_text']:
if hasattr(canvas, attr):
canvas.delete(getattr(canvas, attr))
# Generate button
canvas.generate_btn = round_rectangle(150, 380, 350, 430, r=20, fill=theme_color, outline="")
canvas.generate_text = canvas.create_text(250, 405, text="Generate", font=("Arial", 14, "bold"), fill="white")
canvas.tag_bind(canvas.generate_btn, "<Button-1>", show_random_prompt)
canvas.tag_bind(canvas.generate_text, "<Button-1>", show_random_prompt)
# Close button
canvas.close_btn = round_rectangle(460, 10, 490, 40, r=10, fill=theme_color, outline="")
canvas.close_text = canvas.create_text(475, 25, text="✖", font=("Arial", 12, "bold"), fill="white")
canvas.tag_bind(canvas.close_btn, "<Button-1>", lambda e: root.destroy())
canvas.tag_bind(canvas.close_text, "<Button-1>", lambda e: root.destroy())
# Back button
create_back_btn(theme_color)
# Show main frame
main_frame.tkraise()
def back_to_start():
start_frame.tkraise()
# --- Paths ---
script_dir = os.path.dirname(os.path.abspath(__file__))
csv_path = os.path.join(script_dir, "prompt.csv")
bg_pink = os.path.join(script_dir, "background_pink.png")
bg_blue = os.path.join(script_dir, "background_blue.png")
prompts = load_prompts(csv_path) if os.path.exists(csv_path) else []
# --- Root window ---
root = tk.Tk()
root.geometry("500x499")
root.resizable(False, False)
root.overrideredirect(False)
root.configure(bg='#000')
# Main container
main_container = tk.Frame(root, bg='white', bd=0, highlightthickness=0)
main_container.place(x=2, y=2, width=500, height=499)
# Two stacked frames
start_frame = tk.Frame(main_container, bg="black", width=496, height=495)
main_frame = tk.Frame(main_container, bg="black", width=496, height=495)
for frame in (start_frame, main_frame):
frame.place(x=0, y=0, width=500, height=499)
# --- Start frame ---
title = tk.Label(start_frame, text="Choose Your Theme", font=("Arial", 18, "bold"),
bg="black", fg="white")
title.pack(pady=40)
pink_btn = tk.Button(start_frame, text="Pink Theme", font=("Arial", 14),
bg="#D08F99", fg="black", relief="raised",
command=lambda: start_app("#D08F99", bg_pink))
pink_btn.pack(pady=20, ipadx=10, ipady=5)
blue_btn = tk.Button(start_frame, text="Blue Theme", font=("Arial", 14),
bg="#6CA6CD", fg="black", relief="raised",
command=lambda: start_app("#6CA6CD", bg_blue))
blue_btn.pack(pady=20, ipadx=10, ipady=5)
# --- Main frame ---
canvas = tk.Canvas(main_frame, width=496, height=495, highlightthickness=0, bg="white")
canvas.pack(fill="both", expand=True)
prompt_id = canvas.create_text(
250, 240,
text="Click to get a prompt",
font=("Arial", 16, "bold"),
fill="#000000",
width=300,
justify="center"
)
# Initial buttons (pink theme)
canvas.generate_btn = round_rectangle(150, 380, 350, 430, r=20, fill="#D08F99", outline="")
canvas.generate_text = canvas.create_text(250, 405, text="Generate", font=("Arial", 14, "bold"), fill="white")
canvas.tag_bind(canvas.generate_btn, "<Button-1>", show_random_prompt)
canvas.tag_bind(canvas.generate_text, "<Button-1>", show_random_prompt)
canvas.close_btn = round_rectangle(460, 10, 490, 40, r=10, fill="#D08F99", outline="")
canvas.close_text = canvas.create_text(475, 25, text="✖", font=("Arial", 12, "bold"), fill="white")
canvas.tag_bind(canvas.close_btn, "<Button-1>", lambda e: root.destroy())
canvas.tag_bind(canvas.close_text, "<Button-1>", lambda e: root.destroy())
# Initial back button
create_back_btn("#D08F99")
# --- Show start frame ---
start_frame.tkraise()
# --- Window dragging ---
def start_move(event):
root.x = event.x
root.y = event.y
def stop_move(event):
root.x = None
root.y = None
def on_motion(event):
x = root.winfo_x() + (event.x - root.x)
y = root.winfo_y() + (event.y - root.y)
root.geometry(f"+{x}+{y}")
title.bind("<ButtonPress-1>", start_move)
title.bind("<ButtonRelease-1>", stop_move)
title.bind("<B1-Motion>", on_motion)
drag_area = tk.Frame(start_frame, height=30, bg='black')
drag_area.place(relx=0.5, y=0, anchor='n', relwidth=1.0)
drag_label = tk.Label(drag_area, text="Drag here to move", bg="black", fg="gray", font=("Arial", 8))
drag_label.pack(pady=5)
drag_area.bind("<ButtonPress-1>", start_move)
drag_area.bind("<ButtonRelease-1>", stop_move)
drag_area.bind("<B1-Motion>", on_motion)
root.mainloop()