CustomTkinter
CustomTkinter copied to clipboard
CTkSwitch can not be switch via variable
Here is small GUI to show problem, or maybe I do it wrong:
from functools import partial
import customtkinter
global show_switch
global font_name
def button_callback(app):
print("Button click")
cfg_edit = customtkinter.CTkToplevel(app)
cfg_edit.title('Config Editor')
width, height = 750, 450
cfg_edit.geometry(f'{width}x{height}')
switch_1 = customtkinter.CTkSwitch(master=cfg_edit, text='', variable=show_switch, onvalue=True, offvalue=False)
switch_1.pack(pady=10, padx=10)
entry_1 = customtkinter.CTkEntry(master=cfg_edit, placeholder_text="CTkEntry", textvariable=font_name)
entry_1.pack(pady=10, padx=10)
button_1 = customtkinter.CTkButton(master=cfg_edit, text='Set Switch 2', command=button_switch)
button_1.pack(pady=10, padx=10)
def button_switch():
print('switch')
show_switch.set(True) # <- this not set
font_name.set('second.ttf') # <- this is set
def run(app):
global show_switch
global font_name
show_switch = customtkinter.BooleanVar()
font_name = customtkinter.StringVar()
frame_1 = customtkinter.CTkFrame(master=app)
frame_1.pack(pady=20, padx=60, fill="both", expand=True)
button_1 = customtkinter.CTkButton(master=frame_1, text='Open New', command=partial(button_callback, app))
button_1.pack(pady=10, padx=10)
button_1 = customtkinter.CTkButton(master=frame_1, text='Set Switch 1', command=button_switch)
button_1.pack(pady=10, padx=10)
show_switch.set(True) # <- this not set
font_name.set('first.ttf') # <- this is set
if __name__ == '__main__':
root = customtkinter.CTk()
root.geometry("400x400")
root.title("switch.py")
run(root)
root.mainloop()
First scenarios:
- run script
-
show_switch
is set to True andfont_name
set to'first.ttf'
- click Open New
- only
Entry
was updated - close script
Second scenario:
- run script
-
show_switch
is set to True andfont_name
set to'first.ttf'
- click Open New
- close New opened TopLevel (new window)
- click `Set switch 1'
- click Open New
- again only
Entry
was updated - click 'Set Switch 2`
- now switch is switched
- close script
Info:
Python 3.10 customtkinter 5.0.1 Windows 10 (22H2)