简体   繁体   中英

Make tkinter ttk checkbutton be empty when False

I was making a simple interface with Tkinter, and noticed that when their Checkbutton value is False, or not clicked, it displays a black square covering the button, instead of simply being empty. Example: 例子 . All I wanted is to make the button completely empty when it's state is False, did a bit of digging and couldn't find it. Thanks for your time and help!

Here's the code in question:

querlog = ttk.Checkbutton(window, text='Quero salvar o log', command=quer_salvar_log)
querarquivo = ttk.Checkbutton(window, text='Quero salvar o arquivo de som', command=quer_salvar_som)

You should associate a variable with the checkbutton, and you need to make sure the variable is set to the same value as the offvalue option. The offvalue defaults to zero. You also need to make sure to retain a reference so that the variables don't get destroyed by the garbage collector (ie: don't use local variables)

querlog_var = tk.StringVar(value=0)
querarquivo_var = tk.StringVar(value=0)

querlog = ttk.Checkbutton(window, text='Quero salvar o log', command=quer_salvar_log, variable=querlog_var)
querarquivo = ttk.Checkbutton(window, text='Quero salvar o arquivo de som', command=quer_salvar_som, variable=querarquivo_var)

I noticed the same issue when not using variable= so here is how to do it:

Checkbutton(master, variable='', **kwargs)

You can just set variable attribute as an empty string and that should fix it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM