Tkinter manage state of a button to disable or enable based on number of char entered in Entry field

preview_player
Показать описание
Unless the user enters minimum 4 chars in an entry widget the Button state will be disabled. Once the entry exceeds 4 chars the button will change its state to normal.
To track the change in connected string variable in Entry widget we will use the trace() method.
By using trace method we will trigger the function my_upd() and this function will update the state of the button by reading number of chars entered by the user. To know the length of the chars entered we are using len() function.
To collect the string as entered by the user we have used get() method.

Download the source code from here

#TkinterButtonState #ButtonDisabled #ButtonEnable #ButtonActive #MinimumChars #plus2net #Python
Рекомендации по теме
Комментарии
Автор

Beautifully explained, loved it. Here, I have got a question if you have shortcut to it. Let's say I have 10 to 20 fields in a form and the condition is that we need all entries filled to enable the submit button. We would need write the trace method 20 times for the 20 entries in the form as performed by me. Is there a shorter way of doing this by any chance? 😃

MohitAswani
Автор

thank so much! 😁 greetings from Chile 🇨🇱

estebanmz
Автор

Hello, how can I get the current value of my_str.get() from the my_upd() function in the main program?

timyrasegty
Автор

how can I do this with 7 entry widgets, and a date?

matthewgatdula
Автор

I have an input field which is integer type. Above tutorial works sweet with string data type but for int data type, it throws below error when data is backspaced from the entry box.
_tkinter.TclError: expected floating-point number but got ""

My code is below:

id_value = IntVar()
ID_box = Entry(root, bg="white", fg="black", width=20, textvariable=id_value)
ID_box.place(x=150, y=72)

def check(*args):
if (id_value.get()>0):

else:


id_value.trace('w', check)

search_btn = Button(root, text="Search", bg="White", fg="Black", border=0, font="Arial 10", width=7, height=1, state=DISABLED)
search_btn.pack(pady=50)

Any correction in my method?

MohitAswani