0

I am looking for a way of keeping a button disabled until a valid value is entered into a field. The issue is it has to enable in realtime and without the need for a button to be clicked to check the value.

So for example in the below sample code, if the username of the individual is David1234, the button QR must remain disabled until all characters are entered. So anything other than David1234 been entered will result in the button remianing disabled. Typing David1234 will result in the button becoming active and will become clickable to the user.

import tkinter as tk
import os

#Create QR code window
def QR():
    qr_window = tk.Toplevel(root)
    qr_window.title("QR Code")
    qr_window.geometry('100x100')
    qr_window.resizable(0,0)
    qr_window.config(bg='lightgrey')
    tk.Label(qr_window, text='Test', justify='left', font='arial 11 bold', bg='light grey').pack()

#Change config state of validation label
def valid():
    validation.configure(state='normal')

validated = False

#Create main window
root = tk.Tk()
root.geometry('250x250')
root.resizable(0, 0)
root.title('Client Code Generator')
root.config(bg='lightgrey')

#user input
user_input_QR = tk.StringVar()
tk.Label(root, text='To generate the QR code, please enter your logon id.', font='arial 15 bold',
         wraplength=180, bg='grey80').pack()
entered = tk.Entry(root, font='arial 15', textvariable=user_input_QR, bg='white')
entered.place(x=15, y=130)

#Validation button. Should remain disabled until user enters a valid ID
validation = tk.Button(root, text='QR', state='disabled', command=QR)
validation.place(x=100, y=160)

#####  This is the section I thought would do the trick, but user_input_QR doesn't process automatically #######
if user_input_QR.get() == os.getlogin() and validated == False:
    valid()
    validated = True


root.mainloop()

I can get similar result by implement the validity check into the QR button itself or a separate button, but that requires the button to be 'normal'.

Is there a way to detect entered characters automatically without the user needing to click a button.

David
  • 127
  • 3
  • 10
  • 1
    Does this answer your question? https://stackoverflow.com/questions/4140437/interactively-validating-entry-widget-content-in-tkinter/4140988#4140988 – Bryan Oakley Jun 16 '22 at 19:26
  • @BryanOakley, looks like it might. Let me see if I can understand t a bit more clearly so I may give it a shot. Will get back to you in few. Thanks – David Jun 16 '22 at 19:31
  • @BryanOakley. I struggled to figure out your code but I'm sure it would have satisfied my requirements. I ended up using the StringVar solution by Steven Rumbalski from the same post which worked quite nicely. Thank you for pointing me in the right direction. I'm not sure if this question should be merged, marked as duplicate, or deleted so please apply the appropriate action – David Jun 16 '22 at 21:45

0 Answers0