Guest User

qwen3-coder-30 test

a guest
Sep 15th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.66 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk, messagebox
  3. import requests
  4. import random
  5. from PIL import Image, ImageTk
  6. import io
  7. import threading
  8.  
  9. class KatzenmaedchenViewer:
  10.     def __init__(self, root):
  11.         self.root = root
  12.         self.root.title("Katzenmädchen Viewer")
  13.         self.root.geometry("800x600")
  14.        
  15.         # Create UI elements
  16.         self.create_widgets()
  17.        
  18.         # Load initial image
  19.         self.load_random_image()
  20.        
  21.         # Start auto-refresh timer
  22.         self.start_auto_refresh()
  23.        
  24.     def create_widgets(self):
  25.         # Main frame
  26.         main_frame = ttk.Frame(self.root, padding="10")
  27.         main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
  28.        
  29.         # Configure grid weights
  30.         self.root.columnconfigure(0, weight=1)
  31.         self.root.rowconfigure(0, weight=1)
  32.         main_frame.columnconfigure(1, weight=1)
  33.         main_frame.rowconfigure(2, weight=1)
  34.        
  35.         # Title
  36.         title_label = ttk.Label(main_frame, text="Katzenmädchen Viewer", font=("Arial", 16, "bold"))
  37.         title_label.grid(row=0, column=0, columnspan=3, pady=(0, 10))
  38.        
  39.         # Image display area
  40.         self.image_label = ttk.Label(main_frame)
  41.         self.image_label.grid(row=1, column=0, columnspan=3, pady=10, sticky=(tk.W, tk.E, tk.N, tk.S))
  42.        
  43.         # Progress bar for loading
  44.         self.progress = ttk.Progressbar(main_frame, mode='indeterminate')
  45.         self.progress.grid(row=2, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=(0, 10))
  46.        
  47.         # Buttons frame
  48.         button_frame = ttk.Frame(main_frame)
  49.         button_frame.grid(row=3, column=0, columnspan=3, pady=10)
  50.        
  51.         # Random image button
  52.         self.random_button = ttk.Button(button_frame, text="Load Random Image", command=self.load_random_image)
  53.         self.random_button.pack(side=tk.LEFT, padx=(0, 10))
  54.        
  55.         # Exit button
  56.         self.exit_button = ttk.Button(button_frame, text="Exit", command=self.root.quit)
  57.         self.exit_button.pack(side=tk.LEFT)
  58.        
  59.         # Status label
  60.         self.status_label = ttk.Label(main_frame, text="Ready", foreground="blue")
  61.         self.status_label.grid(row=4, column=0, columnspan=3, pady=(10, 0))
  62.        
  63.     def load_random_image(self):
  64.         # Start loading in a separate thread to prevent UI freezing
  65.         thread = threading.Thread(target=self._load_image_thread)
  66.         thread.daemon = True
  67.         thread.start()
  68.        
  69.     def _load_image_thread(self):
  70.         # Show progress
  71.         self.root.after(0, self.progress.start)
  72.         self.root.after(0, lambda: self.status_label.config(text="Loading image..."))
  73.        
  74.         try:
  75.             # Fetch data from Danbooru API
  76.             # Using the Danbooru API to get posts with the tag "katzenmädchen"
  77.             url = "https://danbooru.donmai.us/posts.json"
  78.             params = {
  79.                 "tags": "3boys",
  80.                 "limit": 100
  81.             }
  82.            
  83.             response = requests.get(url, params=params)
  84.             response.raise_for_status()
  85.             print (response)
  86.             posts = response.json()
  87.            
  88.             if not posts:
  89.                 raise Exception("No images found with the tag 'katzenmädchen'")
  90.            
  91.             # Select a random post
  92.             post = random.choice(posts)
  93.            
  94.             # Get the image URL
  95.             image_url = post.get('file_url') or post.get('large_file_url') or post.get('preview_file_url')
  96.            
  97.             if not image_url:
  98.                 raise Exception("No image URL found")
  99.            
  100.             # Download the image
  101.             image_response = requests.get(image_url)
  102.             image_response.raise_for_status()
  103.            
  104.             # Open image with PIL
  105.             image = Image.open(io.BytesIO(image_response.content))
  106.            
  107.             # Resize image to fit in the display area while maintaining aspect ratio
  108.             max_width = 700
  109.             max_height = 400
  110.             image.thumbnail((max_width, max_height), Image.LANCZOS)
  111.            
  112.             # Convert to PhotoImage for tkinter
  113.             photo = ImageTk.PhotoImage(image)
  114.            
  115.             # Update UI on main thread
  116.             self.root.after(0, self.update_image, photo)
  117.            
  118.         except Exception as e:
  119.             self.root.after(0, lambda: self.status_label.config(text=f"Error: {str(e)}"))
  120.             self.root.after(0, self.progress.stop)
  121.         finally:
  122.             # Stop progress bar
  123.             self.root.after(0, self.progress.stop)
  124.    
  125.     def update_image(self, photo):
  126.         # Update the image display
  127.         self.image_label.config(image=photo)
  128.         self.image_label.image = photo  # Keep a reference to avoid garbage collection
  129.         self.status_label.config(text="Image loaded successfully")
  130.        
  131.     def start_auto_refresh(self):
  132.         """Start the auto-refresh timer"""
  133.         self.auto_refresh_id = self.root.after(10000, self.auto_refresh)
  134.        
  135.     def auto_refresh(self):
  136.         """Called every 10 seconds to load a new random image"""
  137.         self.load_random_image()
  138.         # Schedule the next refresh
  139.         self.start_auto_refresh()
  140.        
  141.     def on_closing(self):
  142.         """Handle window closing event"""
  143.         # Cancel the auto-refresh timer
  144.         if hasattr(self, 'auto_refresh_id'):
  145.             self.root.after_cancel(self.auto_refresh_id)
  146.         self.root.destroy()
  147.  
  148. if __name__ == "__main__":
  149.     root = tk.Tk()
  150.     app = KatzenmaedchenViewer(root)
  151.     root.protocol("WM_DELETE_WINDOW", app.on_closing)
  152.     root.mainloop()
Add Comment
Please, Sign In to add comment