-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
627 lines (519 loc) · 27.5 KB
/
main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
import tkinter as tk
import customtkinter
from CTkMessagebox import CTkMessagebox
import requests
from PIL import Image
import os # Import the 'os' module for file path manipulation
import pyperclip
import io
import win32clipboard
from idlelib.tooltip import Hovertip
from BackgroundColorManager import BackgroundColorManager
from io import BytesIO
class DogImageApp(tk.Tk):
"""
This class represents the main application window for the Dog Image App.
It handles the graphical user interface (GUI), user interaction,
and displays random dog images alongside corresponding cat facts.
"""
def __init__(self):
"""
Initializes the application window with its title, geometry, and widgets.
- Sets the window title to "Dog Diary".
- Disables resizing behavior for a fixed window size.
- Sets the window size to 600x600 pixels.
- Creates frames to organize the layout of UI elements.
- Creates buttons for user interaction with descriptive text.
- Creates labels to display dog images and cat facts.
- Assigns tooltips to buttons using the Hovertip class.
"""
super().__init__()
self.current_image = None # Stores the currently displayed image
background_manager = BackgroundColorManager(self)
self.title("Dog Diary")
self.resizable(False, False)
self.geometry("600x700")
dirname = os.path.dirname(__file__)
#self.icon_path = os.path.join(dirname, 'IconUser.ico') # Replace with your icon file path
#self.iconbitmap(self.icon_path) # Set the window icon
self.warning_ico_path = os.path.join(dirname, 'warning.png')
self.button_frame = tk.Frame(self)
self.image_frame = tk.Frame(self)
self.wisdom_frame = tk.Frame(self)
# "What am I thinking about cats?" button with tooltip
self.button = customtkinter.CTkButton(master=self.button_frame,
text="What am I thinking about cats?",
command=self.show_dog_and_wisdom,
width=300,
text_color="white",
fg_color="#2D1E2F",
hover_color="#F15946")
self.button.grid(row=1, column=1, columnspan=3, padx=2, pady=5)
self.button_tooltip = (
"Unleash the secrets of the feline mind...\n"
"through a dog image and a cat fact!\n"
"Generate a new dog image and cat fact.")
Hovertip(self.button, self.button_tooltip)
# Additional buttons with tooltips
self.new_idea_button = customtkinter.CTkButton(master=self.button_frame,
text="I'll Think Again",
command=self.show_new_idea,
width=150,
text_color="white",
fg_color="#2D1E2F",
hover_color="#F15946")
self.new_idea_button.grid(row=2, column=1, columnspan=1, padx=2, pady=5)
self.new_idea_button_tooltip = (
"Time for a fresh feline thought.\n"
"Displays a new cat fact for the existing dog image.\n"
"Only updates the cat wisdom; the dog image remains the same."
)
Hovertip(self.new_idea_button, self.new_idea_button_tooltip)
# Additional buttons with tooltips
self.copy_image_button = customtkinter.CTkButton(master=self.button_frame,
text="Copy Image",
command=self.copy_image_to_clipboard,
width=150,
text_color="white",
fg_color="#2D1E2F",
hover_color="#F15946")
self.copy_image_button.grid(row=2, column=2, columnspan=1, padx=2, pady=5)
self.copy_image_button_tooltip = "Because sharing is caring!\nCopy the current doggo to your clipboard."
Hovertip(self.copy_image_button, self.copy_image_button_tooltip)
# Additional buttons with tooltips
self.save_button = customtkinter.CTkButton(master=self.button_frame,
text="Save Image",
command=self.save_image,
width=150,
text_color="white",
fg_color="#2D1E2F",
hover_color="#F15946")
self.save_button.grid(row=2, column=3, columnspan=1, padx=2, pady=5)
self.save_button_tooltip = (
"Because who doesn't need more adorable doggo in their life?\n"
"Save the current image to a file.")
Hovertip(self.save_button, self.save_button_tooltip)
self.new_doggo_button = customtkinter.CTkButton(master=self.button_frame,
text="Not My Idea",
command=self.show_new_doggo,
width=150,
text_color="white",
fg_color="#2D1E2F",
hover_color="#F15946")
self.new_doggo_button.grid(row=3, column=1, columnspan=1, padx=2, pady=5)
self.new_doggo_button_tooltip = (
"I am not feeling the cat vibes. How about a different dog?\n"
"Displays a new dog image.\n"
"Only the dog image is updated; the cat wisdom remains the same."
)
Hovertip(self.new_doggo_button, self.new_doggo_button_tooltip)
self.copy_button = customtkinter.CTkButton(
master=self.button_frame,
text="Copy Wisdom",
command=self.copy_wisdom_to_clipboard,
width=150,
text_color="white",
fg_color="#2D1E2F",
hover_color="#F15946",
)
self.copy_button.grid(row=3, column=2, columnspan=1, padx=2, pady=5)
self.copy_button_tooltip = (
"Don't let these wise words go to the dogs!\n"
"Copy the cat fact to your clipboard.")
Hovertip(self.copy_button, self.copy_button_tooltip)
self.save_wisdom_button = customtkinter.CTkButton(
master=self.button_frame,
text="Save Wisdom",
command=self.save_wisdom,
width=150,
text_color="white",
fg_color="#2D1E2F",
hover_color="#F15946",
)
self.save_wisdom_button.grid(row=3, column=3, columnspan=1, padx=2, pady=5)
self.save_wisdom_button_tooltip = (
"Don't let these wise words go to the dogs!\n"
"Save the cat fact to a text file.")
Hovertip(self.save_wisdom_button, self.save_wisdom_button_tooltip)
self.color_label = customtkinter.CTkLabel(master=self.button_frame,
text="Background Color",
width=150,
text_color="#2D1E2F")
self.color_label.grid(row=4, column=1, columnspan=1, padx=2, pady=5)
# Create a dropdown menu for background colors
self.color_menu = customtkinter.CTkComboBox(
master=self.button_frame,
values=list(background_manager.available_colors),
command=lambda color: background_manager.set_color(color),
text_color="white",
fg_color="#2D1E2F",
dropdown_hover_color="#F15946",
button_hover_color="#F15946",
dropdown_fg_color="#2D1E2F",
dropdown_text_color="white",
)
self.color_menu.grid(row=4, column=2, columnspan=1, padx=2, pady=5)
self.dog_image_label = customtkinter.CTkLabel(self.image_frame, text='')
self.dog_image_label.grid(row=1, column=1, columnspan=3, padx=30, pady=10)
self.wisdom_label = customtkinter.CTkLabel(self.wisdom_frame, wraplength=400, text='')
self.wisdom_label.grid(row=2, column=1, columnspan=3, padx=30, pady=10)
self.button_frame.pack(side=tk.TOP, padx=10, pady=10)
self.image_frame.pack(side=tk.TOP, padx=10, pady=10)
self.wisdom_frame.pack(side=tk.TOP, padx=10, pady=10)
def show_new_idea(self):
if not self.current_image:
CTkMessagebox(title="Error",
message="Thinking about cats again? Perhaps a new doggo will inspire you! Press the button 'What am I thinking about cats?' first!",
icon=self.warning_ico_path,
icon_size=(100,100),
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
return
try:
cat_response = requests.get("https://catfact.ninja/fact")
cat_data = cat_response.json()
wisdom_text = cat_data["fact"]
self.show_wisdom(wisdom_text)
except Exception as e:
print(f"An error occurred: {e}")
self.wisdom_label.configure(text="Failed to retrieve cat fact")
def show_new_doggo(self):
"""
Fetches a new random dog image and updates the display.
This method:
1. Fetches a new dog image URL from the dog.ceo API.
2. Updates the displayed image by calling self.show_dog_image()
with the new URL.
"""
wisdom_text = self.wisdom_label.cget("text")
if wisdom_text == "":
CTkMessagebox(title="Error",
message="Press the button 'What am I thinking about cats?' first!",
icon=self.warning_ico_path,
icon_size=(100,100),
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
return
try:
dog_response = requests.get("https://dog.ceo/api/breeds/image/random")
dog_data = dog_response.json()
image_url = dog_data["message"]
self.show_dog_image(image_url)
except Exception as e:
print(f"Error fetching new dog image: {e}")
self.dog_image_label.configure(text="Error fetching new dog image")
def show_dog_and_wisdom(self):
"""
Fetches a random dog image and cat fact concurrently and displays them.
Handles potential errors gracefully to provide informative messages to the user.
This method performs the following steps:
1. Initiates concurrent API requests:
- Fetches a random dog image URL from the "dog.ceo" API.
- Fetches a random cat fact from the "catfact.ninja" API.
2. Parses the JSON responses from both APIs for data extraction.
3. Extracts the dog image URL and cat fact text from the respective responses.
4. Calls helper methods to display the retrieved dog image and cat fact.
- `self.show_dog_image(image_url)`: Displays the downloaded dog image.
- `self.show_wisdom(wisdom_text)`: Displays the retrieved cat fact.
5. Implements error handling with a try-except block:
- Catches any exceptions (e.g., network issues, API errors).
- Logs the error message for debugging purposes.
- Updates the dog image and wisdom labels with informative error messages.
"""
try:
dog_response = requests.get("https://dog.ceo/api/breeds/image/random")
dog_data = dog_response.json()
image_url = dog_data["message"]
cat_response = requests.get("https://catfact.ninja/fact")
cat_data = cat_response.json()
wisdom_text = cat_data["fact"]
self.show_dog_image(image_url)
self.show_wisdom(wisdom_text)
except Exception as e:
print(f"An error occurred: {e}")
self.dog_image_label.configure(text="Error fetching data")
self.wisdom_label.configure(text="Failed to retrieve cat fact")
def show_dog_image(self, image_url):
"""
Downloads and displays the dog image at the given URL.
This method performs the following steps:
1. Downloads the dog image from the provided URL using `requests.get(image_url, stream=True)`.
2. Checks the HTTP status code of the response:
- If successful (status code 200):
- Opens the downloaded image data using `Image.open(image_response.raw)`.
- Stores the processed image in the `self.current_image` attribute for potential future use.
- Calculates the new dimensions to maintain the image's aspect ratio while fitting within the label frame.
- Resizes the image using `image.resize((new_width, new_height))`.
- Converts the PIL image to a format compatible with the CTkImage widget.
- Updates the `dog_image_label` with the resized and converted image.
- If download fails (non-200 status code):
- Logs the error message for debugging purposes.
- Updates the `dog_image_label` with an informative error message.
Args:
image_url (str): The URL of the dog image to download.
"""
image_response = requests.get(image_url, stream=True)
if image_response.status_code == 200:
try:
image = Image.open(image_response.raw)
self.current_image = image # Assign the processed image to current_image
# Calculate the new dimensions to maintain aspect ratio
label_width, label_height = 300, 200 # Target dimensions for the label
image_width, image_height = image.size
# Determine the scaling factor based on the limiting dimension
scale_factor = min(label_width / image_width, label_height / image_height)
# Resize the image while maintaining aspect ratio
new_width = int(image_width * scale_factor)
new_height = int(image_height * scale_factor)
image = image.resize((new_width, new_height))
image_tk = customtkinter.CTkImage(light_image=image, size=(new_width , new_height))
self.dog_image_label.configure(image=image_tk)
self.dog_image_label.image = image_tk
except Exception as e:
print(f"Error processing image: {e}")
self.dog_image_label.configure(text="Error displaying image")
else:
print(f"Error downloading image: {image_response.status_code}")
self.dog_image_label.configure(text="Error downloading image")
def show_wisdom(self, wisdom_text):
"""
Formats and displays the provided cat fact text in the `wisdom_label`.
This method performs the following steps:
1. Assumes `wisdom_text` already contains the retrieved cat fact.
2. Initializes an empty list `lines` to store formatted text with line breaks.
3. Defines `max_length` to control the maximum characters per line for better readability.
4. Iterates through each word in the `wisdom_text`:
- If `lines` is not empty (meaning there's existing formatted text):
- Checks if adding the current word to the last line would exceed `max_length`.
- If exceeded, starts a new line by appending an empty string to `lines`.
- Appends the current word with a space to the last line in `lines`.
5. Joins the formatted lines with newline characters (`\n`) for proper multi-line display.
6. Updates the `wisdom_label` with the formatted text.
Args:
wisdom_text (str): The raw cat fact text to be formatted and displayed.
"""
# No need to repeat the API call here,
# assume `wisdom_text` already contains the fact.
lines = []
max_length = 68
for word in wisdom_text.split():
if lines:
if len(" ".join(lines[-1:])) + len(word) > max_length:
lines.append("")
else:
lines.append("")
lines[-1] = f"{lines[-1]} {word}" # Using f-string here!
self.wisdom_label.configure(text="\n".join(lines))
def save_image(self):
"""
Saves the currently displayed dog image to the user's chosen location.
This method performs the following steps:
1. Checks if there's an image loaded (`self.current_image` is not None).
- If no image is loaded, displays a warning message using `CTkMessagebox`.
- If an image is loaded, proceeds with saving.
2. Prompts the user to choose a file path and filename using `tk.filedialog.asksaveasfilename`.
- Sets the default extension to ".png" and filters for PNG images.
3. If the user selects a file path:
- Attempts to save the loaded image (`self.current_image`) using the PIL library's `save` method.
- On successful save, displays a success message using `CTkMessagebox`.
- Catches potential exceptions during saving:
- Logs the error message for debugging purposes (using `str(e)`).
- Displays an error message to the user using `CTkMessagebox`.
"""
if not self.current_image:
CTkMessagebox(title="Error",
message="No image to save.",
icon=self.warning_ico_path,
icon_size=(100,100),
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
return
# Get the file path where the image will be saved (using a file dialog)
file_path = tk.filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG image", "*.png")])
if file_path:
try:
# Save the image using the PIL library
self.current_image.save(file_path)
CTkMessagebox(title="Success",
message="Image saved successfully.",
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
except FileNotFoundError:
CTkMessagebox(title="Error",
message="Invalid file path. Please choose a valid location.",
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
except OSError as e: # Catching OSError for other file system errors
CTkMessagebox(title="Error",
message=f"A file system error occurred: {e}",
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
except Exception as e: # Catching a general exception if other errors occur
CTkMessagebox(title="Error",
message=f"An unexpected error occurred while saving: {e}",
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
def copy_wisdom_to_clipboard(self):
"""
Copies the currently displayed wisdom text to the system clipboard.
This method performs the following steps:
1. Retrieves the text content from the `wisdom_label` using `cget("text")`.
2. Checks if there's any text to copy:
- If the `wisdom_text` is empty, displays a warning message using `CTkMessagebox`.
3. If there's text to copy:
- Copies the `wisdom_text` to the system clipboard using the `pyperclip.copy` function.
- Displays a success message to inform the user using `CTkMessagebox`.
"""
wisdom_text = self.wisdom_label.cget("text")
if wisdom_text == "":
CTkMessagebox(title="Error",
message="No wisdom text to copy.",
icon=self.warning_ico_path,
icon_size=(100,100),
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
return
pyperclip.copy(wisdom_text)
CTkMessagebox(title="Success",
message="Text has been copied to the clipboard.",
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
def copy_image_to_clipboard(self):
"""Copies the current image to the clipboard in JPG format using win32clipboard.
Args:
self: An instance of the class containing the attribute self.current_image with the image.
"""
if not self.current_image:
CTkMessagebox(title="Error",
message="No image to copy.",
icon=self.warning_ico_path,
icon_size=(100,100),
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
return
# Uložení obrázku do paměti jako JPG
with io.BytesIO() as output:
self.current_image.save(output, format="BMP")
data = output.getvalue()
output = BytesIO()
self.current_image.convert('RGB').save(output, 'BMP')
data = output.getvalue()[14:]
output.close()
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
win32clipboard.CloseClipboard()
CTkMessagebox(title="Success",
message="Image has been copied to the clipboard.",
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
def save_wisdom(self):
"""
Saves the currently displayed wisdom text to a text file.
This method performs the following steps:
1. Retrieves the text content from the `wisdom_label` using `cget("text")`.
2. Checks if there's any text to save:
- If the `wisdom_text` is empty, displays a warning message using `CTkMessagebox`.
3. If there's text to save:
- Prompts the user to choose a file path and filename using `tk.filedialog.asksaveasfilename`.
- Sets the default extension to ".txt" and filters for text files.
4. If the user selects a file path:
- Opens the file in write mode (`"w"`) for writing.
- Writes the `wisdom_text` to the file.
- Closes the file.
- Displays a success message to inform the user using `CTkMessagebox`.
5. Catches potential exceptions during file operations:
- Logs the error message for debugging purposes (using `str(e)`).
- Displays an error message to the user using `CTkMessagebox`.
"""
wisdom_text = self.wisdom_label.cget("text")
if wisdom_text == "":
CTkMessagebox(title="Error",
message="No wisdom text to save.",
icon=self.warning_ico_path,
icon_size=(100,100),
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
return
# Get the file path where the text will be saved (using a file dialog)
file_path = tk.filedialog.asksaveasfilename(
defaultextension=".txt", filetypes=[("Text file", "*.txt")]
)
if file_path:
try:
# Open the file in write mode and write the wisdom text
with open(file_path, "w") as f:
f.write(wisdom_text)
CTkMessagebox(title="Success",
message="Wisdom saved successfully.",
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
except Exception as e:
CTkMessagebox(title="Error",
message=f"An error occurred while saving: {str(e)}",
icon=self.warning_ico_path,
icon_size=(100,100),
fg_color="#F2F2F2",
bg_color="light grey",
button_text_color="white",
button_width=80,
button_color="#2D1E2F",
button_hover_color="#F15946")
# Run the main application loop if this script is executed directly
if __name__ == "__main__":
app = DogImageApp()
app.mainloop()