Untitled
unknown
plain_text
a year ago
7.0 kB
5
Indexable
class SettingsMenu(ctk.CTkFrame):
def __init__(self, parent, dark_mode):
# Calculate window dimensions
window_width = parent.winfo_width()
window_height = parent.winfo_height()
menu_width = max(window_width // 4, 256) # 1/4 of window width or minimum 256px
# Initialize with required dimensions
super().__init__(
parent,
fg_color='#1c1c1e' if dark_mode else '#f5f5f7',
width=menu_width,
height=window_height
)
self.parent = parent
self.dark_mode = dark_mode
self.is_open = False
self.animation_running = False
self.current_width = 0
self.target_width = menu_width
# Place the frame initially off-screen
self.place(relx=1, rely=0, x=menu_width, y=0, anchor='ne')
# Add placeholder content
self.create_menu_content()
# Create settings button
try:
is_light_bg = not dark_mode
icon_suffix = "-b" if is_light_bg else "-w"
icon_image = ctk.CTkImage(
light_image=Image.open(f"icons/settings{icon_suffix}.png"),
dark_image=Image.open(f"icons/settings{icon_suffix}.png"),
size=(30, 30)
)
self.settings_button = ctk.CTkButton(
self.parent,
image=icon_image,
text="",
width=30,
height=30,
fg_color="transparent",
hover_color='#2c2c2e' if dark_mode else '#e5e5e7',
command=self.toggle_menu
)
self.settings_button.place(relx=0.97, rely=0.05, anchor='center')
except Exception as e:
print(f"Error loading settings icon: {e}")
self.settings_button = ctk.CTkButton(
self.parent,
text="⚙",
width=30,
height=30,
fg_color="transparent",
hover_color='#2c2c2e' if dark_mode else '#e5e5e7',
command=self.toggle_menu
)
self.settings_button.place(relx=0.97, rely=0.04, anchor='center')
# Create overlay frame with a compatible semi-transparent color
overlay_color = '#000000' if dark_mode else '#000000'
self.overlay = ctk.CTkFrame(
self.parent,
fg_color=overlay_color,
corner_radius=0,
width=window_width,
height=window_height
)
# Bind click events
self.overlay.bind("<Button-1>", self.handle_click_away)
self.bind("<Button-1>", lambda e: "break") # Prevent clicks on menu from closing
# Bind to parent window resizing
self.parent.bind("<Configure>", self.on_window_resize)
def handle_click_away(self, event):
# Get click coordinates relative to the window
click_x = event.x_root - self.parent.winfo_rootx()
# If click is outside the settings panel area
if click_x < self.parent.winfo_width() - self.target_width:
self.close_menu()
return "break"
def on_window_resize(self, event):
# Update dimensions when window is resized
if self.overlay.winfo_ismapped():
self.overlay.configure(width=event.width, height=event.height)
if self.is_open:
self.target_width = max(event.width // 4, 256)
self.place(relx=1, rely=0, x=0, y=0, anchor='ne', width=self.target_width, height=event.height)
# [Previous create_menu_content method remains the same]
def create_menu_content(self):
"""Create the content of the settings menu"""
# Title
title = ctk.CTkLabel(
self,
text="Settings",
font=("Dongle", 24, "bold"),
text_color='white' if self.dark_mode else 'black'
)
title.pack(pady=(20, 10), padx=20, anchor='w')
# Create a frame for the placeholder content
content_frame = ctk.CTkFrame(
self,
fg_color="transparent",
)
content_frame.pack(fill='both', expand=True, padx=20, pady=10)
# Add placeholder sections with separators
placeholders = [
"General Settings",
"User Preferences",
"System Configuration",
"Display Options",
"Language Settings",
"Security Settings",
"About"
]
for i, text in enumerate(placeholders):
# Add separator before text (except for first item)
if i > 0:
separator = ctk.CTkFrame(
content_frame,
height=2,
fg_color='#2c2c2e' if self.dark_mode else '#e5e5e7'
)
separator.pack(fill='x', pady=(15, 15))
# Add placeholder text
label = ctk.CTkLabel(
content_frame,
text=text,
font=("Dongle", 18),
text_color='white' if self.dark_mode else 'black'
)
label.pack(anchor='w', pady=5)
def toggle_menu(self):
if not self.animation_running:
if self.is_open:
self.close_menu()
else:
self.open_menu()
def open_menu(self, event=None):
if not self.is_open and not self.animation_running:
self.is_open = True
self.animation_running = True
# Show overlay
self.overlay.place(x=0, y=0)
self.lift() # Bring menu to front
self.animate_open()
def close_menu(self, event=None):
if self.is_open and not self.animation_running:
self.is_open = False
self.animation_running = True
self.overlay.place_forget()
self.animate_closed()
def animate_open(self):
if self.current_width < self.target_width:
self.current_width += 40
x_pos = self.target_width - self.current_width
self.place(relx=1, rely=0, x=x_pos, y=0, anchor='ne')
self.after(16, self.animate_open)
else:
self.current_width = self.target_width
self.animation_running = False
def animate_closed(self):
if self.current_width > 0:
self.current_width -= 40
x_pos = self.target_width - self.current_width
self.place(relx=1, rely=0, x=x_pos, y=0, anchor='ne')
self.after(16, self.animate_closed)
else:
self.current_width = 0
self.animation_running = False
self.place(relx=1, rely=0, x=self.target_width, y=0, anchor='ne')
Editor is loading...
Leave a Comment