[Python] How to change a buttons state from disabled to active when an option is selected in...

Discussão em 'Python' iniciado por Stack, Março 8, 2025.

  1. Stack

    Stack Membro Participativo

    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()

    Continue reading...

Compartilhe esta Página