Untitled

 avatar
unknown
plain_text
5 months ago
4.8 kB
4
Indexable
import customtkinter as ctk
import math

class App(ctk.CTk):
    def __init__(self):
        super().__init__()
        
        # Main window configuration
        self.geometry("800x600")
        self.title("Settings Menu Example")
        
        # Create main frame
        self.main_frame = ctk.CTkFrame(self)
        self.main_frame.pack(fill="both", expand=True)
        
        # Create canvas for drawing shapes
        self.canvas = ctk.CTkCanvas(
            self.main_frame,
            width=800,
            height=600,
            bg=self._apply_appearance_mode(self._fg_color),
            highlightthickness=0
        )
        self.canvas.pack(fill="both", expand=True)
        
        # Draw various shapes and elements
        # Circle
        self.canvas.create_oval(50, 50, 150, 150, fill="#1f538d")
        
        # Square
        self.canvas.create_rectangle(200, 50, 300, 150, fill="#2fa572")
        
        # Triangle
        self.canvas.create_polygon(400, 50, 350, 150, 450, 150, fill="#c75d48")
        
        # Star (decorative)
        self.draw_star(600, 100, 50, "#b58d3d")
        
        # Add some clickable buttons across the page
        self.button1 = ctk.CTkButton(
            self.main_frame,
            text="Click Me 1",
            command=lambda: print("Button 1 clicked!")
        )
        self.button1.place(x=100, y=200)
        
        self.button2 = ctk.CTkButton(
            self.main_frame,
            text="Click Me 2",
            command=lambda: print("Button 2 clicked!")
        )
        self.button2.place(x=300, y=300)
        
        self.button3 = ctk.CTkButton(
            self.main_frame,
            text="Click Me 3",
            command=lambda: print("Button 3 clicked!")
        )
        self.button3.place(x=500, y=400)
        
        # Create settings button
        self.settings_button = ctk.CTkButton(
            self.main_frame, 
            text="⚙️", 
            width=40,
            command=self.toggle_settings
        )
        self.settings_button.place(x=740, y=20)
        
        # Create settings panel
        self.settings_panel = ctk.CTkFrame(
            self.main_frame,
            width=200,
            height=600
        )
        
        # Add some example settings
        ctk.CTkLabel(self.settings_panel, text="Settings").pack(pady=10)
        ctk.CTkSwitch(self.settings_panel, text="Dark Mode").pack(pady=5)
        ctk.CTkSwitch(self.settings_panel, text="Notifications").pack(pady=5)
        ctk.CTkButton(self.settings_panel, text="Option 1").pack(pady=5)
        ctk.CTkButton(self.settings_panel, text="Option 2").pack(pady=5)
        ctk.CTkButton(self.settings_panel, text="Option 3").pack(pady=5)
        
        # Track if settings is open
        self.settings_open = False
        
        # Bind click event to the main window
        self.bind("<Button-1>", self.check_click_location)
        
    def draw_star(self, cx, cy, r, fill_color):
        points = []
        for i in range(10):
            angle = math.pi * i / 5
            radius = r if i % 2 == 0 else r/2
            x = cx + radius * math.sin(angle)
            y = cy - radius * math.cos(angle)
            points.extend([x, y])
        self.canvas.create_polygon(points, fill=fill_color)
        
    def toggle_settings(self, event=None):
        if not self.settings_open:
            # Show settings
            self.settings_panel.place(relx=1, rely=0, relwidth=0.25, relheight=1, anchor="ne")
            self.settings_open = True
        else:
            self.close_settings()
    
    def check_click_location(self, event):
        if not self.settings_open:
            return
            
        # Get settings panel position and dimensions
        settings_x = self.settings_panel.winfo_x()
        
        # Get settings button position and dimensions
        button_x = self.settings_button.winfo_x()
        button_y = self.settings_button.winfo_y()
        button_width = self.settings_button.winfo_width()
        button_height = self.settings_button.winfo_height()
        
        # Check if click is on settings button
        is_on_button = (button_x <= event.x <= button_x + button_width and 
                       button_y <= event.y <= button_y + button_height)
        
        # If click is outside settings panel and not on settings button, close it
        if event.x < settings_x and not is_on_button:
            self.close_settings()
    
    def close_settings(self):
        self.settings_panel.place_forget()
        self.settings_open = False

if __name__ == "__main__":
    app = App()
    app.mainloop()
Editor is loading...
Leave a Comment