[英]Condition on Entry Box in Tkinter
How can i insert a condition to check if input in entry box is integer如何插入条件以检查输入框中的输入是否为 integer
i tried the isdigit() method didn't work我试过 isdigit() 方法没用
def appendvalues():
if entry1 != isdigit():
entrymsg1 = tkinter.messagebox.showinfo("Error", "Enter numerical values")
In your case, isinstance(entry1, int)
would work, but you may have some issues if the input is a string, as it will stop the program with the exception ValueError
.在您的情况下, isinstance(entry1, int)
可以工作,但如果输入是字符串,您可能会遇到一些问题,因为它会以异常ValueError
停止程序。
So, my preferred way to handle this is using a try/except statement.因此,我首选的处理方式是使用 try/except 语句。 Here you have an example:这里有一个例子:
def appendvalues():
try:
entry1 = int(entry1)
except ValueError:
# Return an error to the screen
entrymsg1 = tkinter.messagebox.showinfo("Error", "Enter numerical values")
else:
# Use entry1 somewhere
Note that this case, entry1 can be anything, so the user can insert a string (like "1"
or "not a number"
)请注意,在这种情况下,entry1 可以是任何东西,因此用户可以插入一个字符串(如"1"
或"not a number"
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.