-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample3.py
More file actions
49 lines (39 loc) · 1.32 KB
/
Copy pathexample3.py
File metadata and controls
49 lines (39 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
"""
example3: Dynamically Adding and Removing Buttons in a Group
Author: Tchicdje Kouojip Joram Smith (DeltaGa)
Created: Wed Aug 7, 2024
"""
import customtkinter as ctk
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from ctk_toggle.ctk_toggle_button import CTkToggleButton
from ctk_toggle.ctk_toggle_group import CTkToggleGroup
# Create the application window
app = ctk.CTk()
app.geometry("500x400")
# Create a toggle group
group = CTkToggleGroup()
# Add a button dynamically
def add_button():
new_button = CTkToggleButton(
master=app,
text=f"Button {len(group.buttons) + 1}",
toggle_color="#af52de",
command=lambda: print(f"{new_button.cget('text')} is active!")
)
group.add_button(new_button)
new_button.pack(pady=5)
# Remove the last button dynamically
def remove_button():
if group.get_buttons():
last_button = group.buttons[-1]
last_button.destroy()
group.remove_button(last_button)
# Add control buttons to manage dynamic changes
add_button_button = ctk.CTkButton(app, text="Add Button", command=add_button)
remove_button_button = ctk.CTkButton(app, text="Remove Button", command=remove_button)
add_button_button.pack(pady=20)
remove_button_button.pack(pady=10)
app.mainloop()