lsr729
lsr729

Reputation: 832

Disable Checkbutton with if condition

I am trying to make a GUI in which contains combobox and checkbutton. I want the checkbutton to be disabled (gey out) when a specific combobox option is selected.

Following is my code: ( Here I am trying to disable the checkbox1 if the combobox value is 0)

import Tkinter as tk
import ttk

root=tk.Tk()
combo=ttk.Combobox(root,values=['0','1','2'])
combo.set("Select No")
combo.place(relx=0.01,rely=0.4)

var1=IntVar()
check1=tk.Checkbutton(root,text="Select1", variable=var1)
check1.place(relx=0.01,rely=0.6)

var2=IntVar()
check2=tk.Checkbutton(root,text="Select2", variable=var2)
check2.place(relx=0.4,rely=0.6)

if combo.get()=='0':
    check1.config(state=tk.DISABLED)

root.mainloop()

Upvotes: 0

Views: 159

Answers (1)

FrainBr33z3
FrainBr33z3

Reputation: 1105

You can use the ComboboxSelected event bind as follows:

def disable(event):
    if combo.get()=='0':
        check1.config(state = tk.DISABLED)
combo.bind("<<ComboboxSelected>>", disable)

The callback function disable is invoked each time the value of the Combobox is changed

Upvotes: 1

Related Questions