0

I cannot find a solution to the following problem. Given is a tkinter application with 4 entry widgets a,b,c, and d that must fulfill the following conditions:

Only numbers may be entered in entry a and there must not be more than 4 digits

If a is empty then no input can be made in c. The contents of c and d are the same as b.

If a is not empty then an entry can be made in c. The content of c and d is the same (they are unlinked from b).

The current solutions works only partially. It is able to link entry b and c and unlink them. But I don't know how to include the 3 condition.

from tkinter import *

root = Tk()
root.geometry("200x200")


def only_numeric_input(P):
    if len(P) > 0 and P.isdigit():
        # enable entry_c and unlink its content from entry_b
        entry_c.config(textvariable=" ", state='normal')
    else:
        # disable entry_c
        entry_c.config(textvariable=var_b, state='disabled')

    if len(P) > 4:
        return False
    # checks if entry's value is an integer or empty and returns an appropriate boolean
    if P.isdigit() or P == "":  # if a digit was entered or nothing was entered
        return True
    return False

callback = root.register(only_numeric_input)  # registers a Tcl to Python callback

var_b = StringVar()
var_c = StringVar()

Label(root, text="a").grid(row = 0, column = 0, pady = (10,0))
Label(root, text="b").grid(row = 1, column = 0)
Label(root, text="c").grid(row = 2, column = 0)
Label(root, text="d").grid(row = 3, column = 0, pady = (40,0))

entry_a = Entry(root)
entry_b = Entry(root, textvariable = var_b)
entry_c = Entry(root, textvariable = var_b, state = "disabled")
entry_d = Entry(root, textvariable = var_b)

#display entrys
entry_a.grid(row = 0, column = 1)
entry_b.grid(row = 1, column = 1)
entry_c.grid(row = 2, column = 1)
entry_d.grid(row = 3, column = 1, pady = (40,0))

entry_a.configure(validate="key", validatecommand=(callback, "%P"))  # enables validation

mainloop()
8
  • 1
    You really aren't wanting to do what I would call dynamic Entrys — it sounds to me that you want to do validation of what is being entered into them. The good news is tkinter Entry widgets support validation. Here's some slightly dated documentation (I suspect you may be able to find other sources now that you now know what term to look for). Commented Aug 18, 2020 at 18:14
  • @martineau will validation be helpful if you want to enter the digits into entrybox and check weather each digit is correct, without using a button? Commented Aug 18, 2020 at 18:27
  • Yes, because you can set validation up so that every 'key' triggers a call to the callback function (as well as various other events mentioned in the documentation). Commented Aug 18, 2020 at 18:37
  • @martineau oh thanks ill look up on validation. Micheal, i was asking a Q from my part, as im new to validation, nothing related to this post Commented Aug 18, 2020 at 18:41
  • @Michael: Yes, but not with a substitution code that would specify what was being inserted or deleted (which is effectively enables it per keystroke). In other words, the "%S" code. Commented Aug 18, 2020 at 18:45

1 Answer 1

1

Here ya go. You were using isdigit for P but %P is the whole text (including what was just pressed), so we switch to isnumeric. If I understood your instructions properly, you forgot to handle entry_d.

I used a range instead of len(P) > 0 and len(P) < 5, and the range is correct.

def only_numeric_input(P):
    if len(P) in range(1,5) and P.isnumeric():
        #if we have 1 to 4 numeric characters
        # enable entry_c, and unlink entry c & d content from entry b
        entry_c.config(textvariable=var_c, state='normal')
        entry_d.config(textvariable=var_c)
    elif not P:
        #if we have no characters
        # disable entry_c, and link entry c & d content to entry b
        entry_c.config(textvariable=var_b, state='disabled')
        entry_d.config(textvariable=var_b)
    else:
        #everything else
        return False
    
    return True
Sign up to request clarification or add additional context in comments.

1 Comment

Sry for the late reply. I just implemented your code and its work perfectly! Thank your very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.