Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk, messagebox
- import requests
- import random
- from PIL import Image, ImageTk
- import io
- import threading
- class KatzenmaedchenViewer:
- def __init__(self, root):
- self.root = root
- self.root.title("Katzenmädchen Viewer")
- self.root.geometry("800x600")
- # Create UI elements
- self.create_widgets()
- # Load initial image
- self.load_random_image()
- # Start auto-refresh timer
- self.start_auto_refresh()
- def create_widgets(self):
- # Main frame
- main_frame = ttk.Frame(self.root, padding="10")
- main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
- # Configure grid weights
- self.root.columnconfigure(0, weight=1)
- self.root.rowconfigure(0, weight=1)
- main_frame.columnconfigure(1, weight=1)
- main_frame.rowconfigure(2, weight=1)
- # Title
- title_label = ttk.Label(main_frame, text="Katzenmädchen Viewer", font=("Arial", 16, "bold"))
- title_label.grid(row=0, column=0, columnspan=3, pady=(0, 10))
- # Image display area
- self.image_label = ttk.Label(main_frame)
- self.image_label.grid(row=1, column=0, columnspan=3, pady=10, sticky=(tk.W, tk.E, tk.N, tk.S))
- # Progress bar for loading
- self.progress = ttk.Progressbar(main_frame, mode='indeterminate')
- self.progress.grid(row=2, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=(0, 10))
- # Buttons frame
- button_frame = ttk.Frame(main_frame)
- button_frame.grid(row=3, column=0, columnspan=3, pady=10)
- # Random image button
- self.random_button = ttk.Button(button_frame, text="Load Random Image", command=self.load_random_image)
- self.random_button.pack(side=tk.LEFT, padx=(0, 10))
- # Exit button
- self.exit_button = ttk.Button(button_frame, text="Exit", command=self.root.quit)
- self.exit_button.pack(side=tk.LEFT)
- # Status label
- self.status_label = ttk.Label(main_frame, text="Ready", foreground="blue")
- self.status_label.grid(row=4, column=0, columnspan=3, pady=(10, 0))
- def load_random_image(self):
- # Start loading in a separate thread to prevent UI freezing
- thread = threading.Thread(target=self._load_image_thread)
- thread.daemon = True
- thread.start()
- def _load_image_thread(self):
- # Show progress
- self.root.after(0, self.progress.start)
- self.root.after(0, lambda: self.status_label.config(text="Loading image..."))
- try:
- # Fetch data from Danbooru API
- # Using the Danbooru API to get posts with the tag "katzenmädchen"
- url = "https://danbooru.donmai.us/posts.json"
- params = {
- "tags": "3boys",
- "limit": 100
- }
- response = requests.get(url, params=params)
- response.raise_for_status()
- print (response)
- posts = response.json()
- if not posts:
- raise Exception("No images found with the tag 'katzenmädchen'")
- # Select a random post
- post = random.choice(posts)
- # Get the image URL
- image_url = post.get('file_url') or post.get('large_file_url') or post.get('preview_file_url')
- if not image_url:
- raise Exception("No image URL found")
- # Download the image
- image_response = requests.get(image_url)
- image_response.raise_for_status()
- # Open image with PIL
- image = Image.open(io.BytesIO(image_response.content))
- # Resize image to fit in the display area while maintaining aspect ratio
- max_width = 700
- max_height = 400
- image.thumbnail((max_width, max_height), Image.LANCZOS)
- # Convert to PhotoImage for tkinter
- photo = ImageTk.PhotoImage(image)
- # Update UI on main thread
- self.root.after(0, self.update_image, photo)
- except Exception as e:
- self.root.after(0, lambda: self.status_label.config(text=f"Error: {str(e)}"))
- self.root.after(0, self.progress.stop)
- finally:
- # Stop progress bar
- self.root.after(0, self.progress.stop)
- def update_image(self, photo):
- # Update the image display
- self.image_label.config(image=photo)
- self.image_label.image = photo # Keep a reference to avoid garbage collection
- self.status_label.config(text="Image loaded successfully")
- def start_auto_refresh(self):
- """Start the auto-refresh timer"""
- self.auto_refresh_id = self.root.after(10000, self.auto_refresh)
- def auto_refresh(self):
- """Called every 10 seconds to load a new random image"""
- self.load_random_image()
- # Schedule the next refresh
- self.start_auto_refresh()
- def on_closing(self):
- """Handle window closing event"""
- # Cancel the auto-refresh timer
- if hasattr(self, 'auto_refresh_id'):
- self.root.after_cancel(self.auto_refresh_id)
- self.root.destroy()
- if __name__ == "__main__":
- root = tk.Tk()
- app = KatzenmaedchenViewer(root)
- root.protocol("WM_DELETE_WINDOW", app.on_closing)
- root.mainloop()
Add Comment
Please, Sign In to add comment