Untitled
unknown
plain_text
a year ago
4.6 kB
6
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)
# Add shapes
self.canvas.create_oval(50, 50, 150, 150, fill="#1f538d")
self.canvas.create_rectangle(200, 50, 300, 150, fill="#2fa572")
self.canvas.create_polygon(400, 50, 350, 150, 450, 150, fill="#c75d48")
self.draw_star(600, 100, 50, "#b58d3d")
# Add example buttons
self.button1 = ctk.CTkButton(self.main_frame, text="Click Me 1")
self.button1.place(x=100, y=200)
self.button2 = ctk.CTkButton(self.main_frame, text="Click Me 2")
self.button2.place(x=300, y=300)
self.button3 = ctk.CTkButton(self.main_frame, text="Click Me 3")
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 (initially hidden)
self.settings_panel = ctk.CTkFrame(self.main_frame, width=200, height=600)
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)
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):
if not self.settings_open:
# Show settings
self.settings_panel.place(relx=1, rely=0, anchor="ne")
self.update_idletasks() # Ensure geometry updates
self.settings_open = True
else:
self.close_settings()
def check_click_location(self, event):
if not self.settings_open:
return
# Get settings panel dimensions
settings_x = self.settings_panel.winfo_x()
settings_y = self.settings_panel.winfo_y()
settings_width = self.settings_panel.winfo_width()
settings_height = self.settings_panel.winfo_height()
# Check if click is within the settings panel
is_in_settings = (
settings_x <= event.x <= settings_x + settings_width and
settings_y <= event.y <= settings_y + settings_height
)
# Get settings button 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 the 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 button, close settings
if not is_in_settings 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