Home > Blockchain >  How to disable the following button in Tkinter?
How to disable the following button in Tkinter?

Time:06-23

I have a hard time trying to disable the buttons I don't need in Tkinter, until some condition is met.

The following code it's a reference of what I'm doing:

ID_Personal=[]
ID_Product=[]

from tkinter import *

window = Tk()

def addProduct():
    def callback():
            ID_Product.append(ID_Product_Entry.get())
            print("Product registered")
    
    windowProduct = Tk()
    lblProduct = Label(windowProduct, text="ID: ")
    lblProduct.grid(padx=10, pady=10, row=0, column=0)
    
    ID_Product_Entry = Entry(windowProduct)
    ID_Product_Entry.grid(padx=10, pady=10, row=0, column=1)
    
    btnAdd = Button(windowProduct, text="Submit",command=callback)
    btnAdd.grid(padx=10, pady=10, row=1, column=0)
    windowProduct.mainloop()

def addPersonal():
    def callbackPersonal():
            ID_Personal.append(ID_Personal_Entry.get())
            print("Employee registered")
    
    windowPersonal = Tk()
    lblProduct = Label(windowPersonal, text="ID: ")
    lblProduct.grid(padx=10, pady=10, row=0, column=0)

    ID_Personal_Entry = Entry(windowPersonal)
    ID_Personal_Entry.grid(padx=10, pady=10, row=0, column=1)

    btnAddP = Button(windowPersonal, text="Submit",command=callbackPersonal)
    btnAddP.grid(padx=10, pady=10, row=1, column=0)
    windowPersonal.mainloop()
    
btnProduct = Button(window,text="Product",command=addProduct)
btnProduct.grid(padx=10, pady=10, row=0, column=0)
        
btnPersonal = Button(window,text="Personal",command=addPersonal)
btnPersonal.grid(padx=10, pady=10, row=1, column=0)

window.mainloop()

Basically what I need to do is disable the button btnProduct when my ID_Personal list it's empty. Once some personal has been registered, then it must be active again. I've been trying but nothing seems to work. I don't know if I'm using the if condition wrongly or what. Thank you in advance for your help.

CodePudding user response:

Set the state disabled when you create the button. btnProduct = Button(window,text="Product",command=addProduct, state="disabled")

And in the callbackPersonal function set the state active. btnProduct.config(state="active")

This way when you run the program the product button will be disabled, but once you add your first personal, the button will be active.

CodePudding user response:

You can set the state of btnProduct to "disabled" when you create it, as the list ID_Personal is initially empty.

btnProduct = Button(window,text="Product",command=addProduct,state='disabled')

You can then change the button state to "normal" in the callbackPersonal function using btnProduct.config(state='normal') This will normalize the button the first time an item is added to ID_Personal

Now if your app demands that the button should go back to "disabled" if ID_Personal is empty and turn "normal" only when ID_Personal is non-empty then the above solution won't work. In that case, we need to recursively update the widgets based on changing conditions. The "after" function can help us to achieve that.

def update():
    if len(ID_Personal) > 0:
        btnProduct.config(state='normal')
    else:
        btnProduct.config(state='disable')
    window.after(1,update)
    
update()

Adding the above code to your program will help you update your widgets (button in this case) continuously after every 1 millisecond.

  • Related