CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

CTkSwitch can not be switch via variable

Open emcek opened this issue 1 year ago • 0 comments

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:

  1. run script
  2. show_switch is set to True and font_name set to 'first.ttf'
  3. click Open New
  4. only Entry was updated
  5. close script

Second scenario:

  1. run script
  2. show_switch is set to True and font_name set to 'first.ttf'
  3. click Open New
  4. close New opened TopLevel (new window)
  5. click `Set switch 1'
  6. click Open New
  7. again only Entry was updated
  8. click 'Set Switch 2`
  9. now switch is switched
  10. close script

Info:

Python 3.10 customtkinter 5.0.1 Windows 10 (22H2)

emcek avatar Dec 04 '22 22:12 emcek