lsr729
lsr729

Reputation: 832

Disable some options from Tkiner combobox with if condition

I am trying to build a simple gui, which has 2 comboboxes (say combo1 and combo2).

I want some values of combo1 to be disabled/deactivated when some specific values of combo2 are selected.

The following is a sample code:

import ttk


root=tk.Tk()
c1_val=['0','1','2','3']
c2_val=['a','b','c']

combo1=ttk.Combobox(root,values=c1_val)
combo1.set("Select No")
combo1.place(relx=0.01,rely=0.4)

combo2=ttk.Combobox(root,values=c2_val)
combo2.set("Select No")
combo2.place(relx=0.01,rely=0.5)




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

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



root.mainloop()

In the above script, I want the values of combo1 = ['0','1'] if combo2= ['a'].

What function can I make here to achieve that?

Upvotes: 1

Views: 889

Answers (1)

FrainBr33z3
FrainBr33z3

Reputation: 1105

Following is an example to set the value of a Combobox:

import tkinter as tk
from tkinter import ttk

def setValue(event):
     print(combo.set('January'))

app = tk.Tk() 
app.geometry('400x100')

labelTop = tk.Label(app,
                    text = "Choose your favourite month")
labelTop.grid(column=0, row=0)

combo = ttk.Combobox(app, 
                            values=[
                                    "January", 
                                    "February",
                                    "March",
                                    "April",
                                    "May"])


combo.grid(column=0, row=1)
combo.current(1)
combo.bind("<<ComboboxSelected>>", setValue)
app.mainloop()

Apply this logic to your code and it should work

UPDATE

To re-assign the values of a particular Combobox, try the following: (Using the same example as above)

def setValue(event):
    if combo.get()=='February':
        combo['values'] = ['January', 'May']
    else:
        combo['values'] = ['January', 'February', 'March', 'April', 'May']

Upvotes: 1

Related Questions