学习Entry组件问题
在学习Entry组件时,碰到invalidatecommand参数报错如下:_tkinter.TclError: unknown option "-invalidatecommand"
请大虾指点,谢谢!
具体代码如下:
root = Tk()
Label(root, text='账号:').grid(row=0)# 第一行位置第一列
Label(root, text='密码:').grid(row=1)# 第二行位置第一列
v1 = StringVar()
v2 = StringVar()
def test():
if e1.get().isalnum(): # 字母和数字时为真
return True
else:
e1.delete(0, END)
Label(root, text='请重新输入用户名').grid(row=2)
return False
def test1():
Label(root, text='我被调用了').grid(row=3)
return True
# Entry组件还支持验证输入内容的合法性。例如输入框要求输入的是数字,用户输入了字母那就属于“非法”。
# 实现该功能,需要通过设置validate(启用验证)、validatecommand(指定验证函数)和invalidcommand(当validatecommand的返回值为False的时候才被调用)三个选项
e1 = Entry(root, textvariable=v1, validate='focusout', validatecommand=test, invalidatecommand=test1)# validate='focusout/focusin/focus/key/all/none'
e2 = Entry(root, textvariable=v2, show='*') # 输入框内容隐藏显示*
e1.grid(row=0, column=1, padx=10, pady=5) # 第一行第二列
e2.grid(row=1, column=1, padx=10, pady=5) # 第二行第二列
def show():
print('账号:%s' % e1.get())
print('密码::%s' % e2.get())
e1.delete(0, END)
e2.delete(0, END)
Button(root, text='芝麻开门', width=10, command=show).grid(row=3, column=0, sticky=W, padx=10, pady=5)
Button(root, text='退出', width=10, command=root.quit).grid(row=3, column=1, sticky=E, padx=10, pady=5)
mainloop() 把 invalidatecommand 换成 invalidcommand
from tkinter import *
root = Tk()
Label(root, text='账号:').grid(row=0)# 第一行位置第一列
Label(root, text='密码:').grid(row=1)# 第二行位置第一列
v1 = StringVar()
v2 = StringVar()
def test():
if e1.get().isalnum(): # 字母和数字时为真
return True
else:
e1.delete(0, END)
Label(root, text='请重新输入用户名').grid(row=2)
return False
def test1():
Label(root, text='我被调用了').grid(row=3)
return True
# Entry组件还支持验证输入内容的合法性。例如输入框要求输入的是数字,用户输入了字母那就属于“非法”。
# 实现该功能,需要通过设置validate(启用验证)、validatecommand(指定验证函数)和invalidcommand(当validatecommand的返回值为False的时候才被调用)三个选项
e1 = Entry(root, textvariable=v1, validate='focusout', validatecommand=test, invalidcommand=test1)# validate='focusout/focusin/focus/key/all/none'
e2 = Entry(root, textvariable=v2, show='*') # 输入框内容隐藏显示*
e1.grid(row=0, column=1, padx=10, pady=5) # 第一行第二列
e2.grid(row=1, column=1, padx=10, pady=5) # 第二行第二列
def show():
print('账号:%s' % e1.get())
print('密码::%s' % e2.get())
e1.delete(0, END)
e2.delete(0, END)
Button(root, text='芝麻开门', width=10, command=show).grid(row=3, column=0, sticky=W, padx=10, pady=5)
Button(root, text='退出', width=10, command=root.quit).grid(row=3, column=1, sticky=E, padx=10, pady=5)
mainloop() 《请大虾指点》 wyhpylc 发表于 2023-2-14 19:12
《请大虾指点》
请不要误会,大虾是以前的网络用语,意思是电脑高手 liuhongrun2022 发表于 2023-2-14 21:02
请不要误会,大虾是以前的网络用语,意思是电脑高手
az,我网龄比较短,抱歉抱歉,我以为是打错字了{:10_266:} wyhpylc 发表于 2023-2-14 21:09
az,我网龄比较短,抱歉抱歉,我以为是打错字了
暴露网龄了 pysunred 发表于 2023-2-15 11:46
暴露网龄了
是的,本人初二,要不是学编程可能现在还不会用电脑)
页:
[1]