我找不到以下问题的解决方案。a
给定的是一个带有 4 个入口小部件, b
, c
,的 tkinter 应用程序,d
它必须满足以下条件:
条目中只能输入数字a
,且不得超过 4 位数字
如果a
为空则无法在 中进行输入c
。c
和的内容d
与 相同b
。
如果a
不为空,则可以在 中进行输入c
。c
和的内容d
相同(它们与 未链接b
)。
当前的解决方案仅部分起作用。它能够链接条目b
和c
取消链接它们。但我不知道如何包含 3 条件。
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()
繁星点点滴滴
相关分类