[英]How to check the validity of a string input from GUI in python
I'm taking the input for a name in Tkinter, but it's giving an error when I want to check if only alphabets are entered:我正在 Tkinter 中输入名称,但是当我想检查是否只输入了字母时,它给出了一个错误:
t1=Label(text="Name:")
t1.place(x=40,y=100)
name=Entry(t)
if name.isalpha():
print("Valid")
else:
print("Invalid")
How do I check the validity of the input?如何检查输入的有效性?
You want to check the value of your input field.您想检查输入字段的值。 Use
name.get().isalpha()
.使用
name.get().isalpha()
。
name
is just a instance of Entry. name
只是 Entry 的一个实例。 You want the value of this entry field.您需要此输入字段的值。 You can get this by calling
get()
on the Entry instance.您可以通过在 Entry 实例上调用
get()
来获得它。
isalpha()
is defined on str
ings only. isalpha()
仅在str
上定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.