Skip to content

I need help: Add like a Computer Desktop Mode #43

@AnonymousVibrate

Description

@AnonymousVibrate

Sir, Is is possible to make like a Computer Desktop Style with this ? I don't know where to start.

I want to make a CTK act like a Computer Desktop Window and I want that a CTkToplevel was inside the Main window, but Top-level is a separated window, so I just make a frame that is draggable inside the Desktop window; In short, CTK as Main window and Frame should be like an Application that will run inside the Desktop window; I made this code so far but I'm not satisfied with it. I don't like the performance of the resizing cuz it's flickering; I know this is out of the topic from your current repo, sorry about that.

import customtkinter as ctk

# Initialize the main window
root = ctk.CTk()
root.title("CTk Main Window")
root.geometry("800x600")

# Variables to track mouse position and frame state
resize_start_x, resize_start_y = 0, 0
frame_width, frame_height = 400, 300  # Default width and height
frame_x, frame_y = 200, 150  # Default position

# Functionality for dragging the custom frame
def start_move(event):
    global x, y
    x, y = event.x, event.y

def on_drag(event):
    global frame_x, frame_y
    dx, dy = event.x - x, event.y - y
    frame_x = custom_frame.winfo_x() + dx
    frame_y = custom_frame.winfo_y() + dy
    custom_frame.place(x=frame_x, y=frame_y)

# Resize functionality
def start_resize(event):
    global resize_start_x, resize_start_y, frame_width, frame_height
    resize_start_x, resize_start_y = event.x, event.y
    frame_width = custom_frame.winfo_width()
    frame_height = custom_frame.winfo_height()

def on_resize(event):
    global frame_width, frame_height
    dx, dy = event.x - resize_start_x, event.y - resize_start_y
    frame_width = max(200, frame_width + dx)  # Minimum width = 200
    frame_height = max(150, frame_height + dy)  # Minimum height = 150
    custom_frame.place_configure(width=frame_width, height=frame_height)

# Close functionality
def close_frame():
    custom_frame.place_forget()

# Minimize functionality
def minimize_frame():
    custom_frame.place_forget()

# Restore frame functionality
def restore_frame():
    custom_frame.place(x=frame_x, y=frame_y, width=frame_width, height=frame_height)

# Create a custom frame (like a window app inside the main CTk window)
custom_frame = ctk.CTkFrame(root, width=frame_width, height=frame_height, corner_radius=10)
custom_frame.place(x=frame_x, y=frame_y)

# Title bar for the custom frame
title_bar = ctk.CTkFrame(custom_frame, height=30, corner_radius=0, fg_color="#444444")
title_bar.pack(side="top", fill="x")

# Close and Minimize buttons
btn_close = ctk.CTkButton(title_bar, text="X", fg_color="#FF5555", text_color="white", width=30, command=close_frame)
btn_minimize = ctk.CTkButton(title_bar, text="_", fg_color="#5555FF", text_color="white", width=30, command=minimize_frame)

btn_close.pack(side="right", padx=5, pady=2)
btn_minimize.pack(side="right", padx=5, pady=2)

# Dragging events for the custom frame
title_bar.bind("<Button-1>", start_move)
title_bar.bind("<B1-Motion>", on_drag)

# Resizing grip at the bottom-right corner
resize_grip = ctk.CTkFrame(custom_frame, width=15, height=15, corner_radius=0, fg_color="#777777")
resize_grip.place(relx=1.0, rely=1.0, anchor="se")

resize_grip.bind("<Button-1>", start_resize)
resize_grip.bind("<B1-Motion>", on_resize)

# Content inside the custom frame
content_label = ctk.CTkLabel(custom_frame, text="This is a resizable and draggable CTk custom frame.", font=("Arial", 14))
content_label.pack(pady=20)

# Restore button to reopen the frame
restore_button = ctk.CTkButton(root, text="Restore Frame", command=restore_frame)
restore_button.pack(pady=10)

# Run the CTk main loop
root.mainloop()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions