我在限制条目窗口小部件中的字符串长度方面遇到了问题。
我看过几篇关于validatecommand
的文章,但并不安静地理解它们。
任何人都可以向我解释如何使用该功能,或者是否还有其他更好的方法可以做到这一点?
我把我的代码放在
之下from tkinter import *
class Window:
def __init__(self, master):
self.master = master
master.title("password")
master.geometry("1430x1000")
master.label = Label(master, text="Enter the password and press the button to save it", font=("Times", 30, "bold"), relief=RIDGE, bd=6, bg="grey")
master.label.grid(row=0, column=1, padx=3, ipadx=3, pady=3, sticky=W+E)
Ent.entry(master)
Save.button(master)
global if_entered
global password_display
if_entered = Label(master, text="",font=("Arial", 25) )
if_entered.grid(row=3, column=1)
password_display = Label(master, text="", font=("Arial", 25))
password_display.grid(row=4, column=1)
class Ent:
def entry(self):
global aa
aa = StringVar()
global entered_password
entered_password = Entry(self, font=("Arial", 30), textvariable=aa)
entered_password.grid(row=1, column=1)
class Save:
def button(self):
button_save = Button(self, text="save", command=Save.save_callback)
button_save.grid(row=2, column=1)
Label(self, text="Your new password :", font=("Arial", 20, "bold")).grid(column=0, row=4, ipadx=0)
def save_callback(self=None):
global password
password = aa.get()
if len(password) <1: #0
print("A")
if_entered.configure(text="not entered", font=("Arial", 25))
if_entered.grid(row=3, column=1)
password_display.configure(text="none entered",font=("Arial", 25), bg="yellow")
password_display.grid(row=4, column=1)
elif len(password) >0: #bigger than 0
if_entered.configure(text="password saved", font=("Arial",25))
if_entered.grid(row=3, column=1)
print(password)
password_display.configure(text=password, font=("Arial", 25), bg="yellow")
password_display.grid(row=4, column=1)
root = Tk()
win = Window(root)
root.mainloop()