0

I'm trying to change the button state when an option is selected in the option menu, but nothing is changing. Can anyone tell me what I'm doing wrong?

from tkinter import *

def setLabel():
    changed.set("Active")
    
def changeState():
    pick = choose.get()
    if (pick == "op2"):
        button['state'] = button.ACTIVE
        button.config(text = "ACTIVE")
    else:
        button['state'] = app.DISABLED
        button.config(text = "Disabled")

app = Tk()
app.resizable(40,40)

choose = StringVar()
choose.set("op1")
options = OptionMenu(app, choose, "op1", "op2")
options.pack()


button = Button(app, text = "Disabled", state = DISABLED, command = setLabel)
button.pack()

changed = StringVar()
label = Label(app, textvariable = changed, font = ("helvetica", 10))
label.pack()

app.mainloop()

3 Answers3

0

Change your function to this:

def changeState():
    pick = choose.get()
    if (pick == "op2"):
        button['state'] = ACTIVE #means active state
        button.config(text = "ACTIVE")
    else:
        button['state'] = DISABLED #means disabled state
        button.config(text = "Disabled")

Also your not calling your function, so to call it and make the effect active, add a command argument onto your optionmenu, like:

options = OptionMenu(app, choose, "op1", "op2",command=lambda _:changeState())

Using lambda _: because optionmenu command expect a tkinter variable to be passed on, so as to avoid that. You could also have a parameter for your function, but if your calling your function somewhere else, your gonna have to pass in a argument or your could also use parameter like point=None and get rid of the lambda.

Hope this cleared the errors, do let me know if any doubts.

Cheers

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
0

Got it!

Edit: I changed line 9 slightly, removing button.active and replacing with NORMAL and this worked.

from tkinter import *

def setLabel():
    changed.set("Active")
    
def changeState(*args):
    pick = choose.get()
    if (pick == "op2"):
        button['state'] = NORMAL
        button.config(text = "ACTIVE")
    else:
        button['state'] = DISABLED
        button.config(text = "Disabled")

app = Tk()
app.resizable(40,40)

choose = StringVar()
choose.set("op1")
options = OptionMenu(app, choose, "op1", "op2")
choose.trace("w", changeState)
options.pack()


button = Button(app, text = "Disabled", state = DISABLED, command = setLabel)
button.pack()

changed = StringVar()
label = Label(app, textvariable = changed, font = ("helvetica", 10))
label.pack()

app.mainloop()
  • Code-only answers are discouraged here. Please [edit] it and say what you did to fix the problem. – martineau Sep 25 '20 at 11:12
  • It's a code-only answer to your own question, but you have said what you "got" or what's different from the code in your question — so it makes it less enlightening to others. – martineau Sep 25 '20 at 11:41
0

You can add command=changeState in OptionMenu(...) to call changeState() whenever the options is changed:

def changeState(pick):
    if pick == "op2":
        button['state'] = ACTIVE
        button.config(text = "ACTIVE")
    else:
        button['state'] = DISABLED
        button.config(text = "Disabled")

...

options = OptionMenu(app, choose, "op1", "op2", command=changeState)
acw1668
  • 40,144
  • 5
  • 22
  • 34