如果用户在 Python/Tkinter 中输入字符串,如何告诉用户只使用整数? [英] How to tell user to only use integers if they input string in Python / Tkinter?

查看:21
本文介绍了如果用户在 Python/Tkinter 中输入字符串,如何告诉用户只使用整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出错误并告诉用户仅在输入字母作为输入时输入数字的最佳方法是什么?不起作用的代码:

What would be the best way to give an error and tell the user to only input numbers if they type letters as an input? Code that doesn't work:

if self.localid_entry.get() == int(self.localid_entry.get():
                self.answer_label['text'] = "Use numbers only for I.D."

该变量在 Tkinter 中获得:

The variable is obtained in Tkinter with:

    self.localid2_entry = ttk.Entry(self, width=5)
    self.localid2_entry.grid(column=3, row=2)

推荐答案

是这样的:

try:
    i = int(self.localid_entry.get())

except ValueError:
    #Handle the exception
    print 'Please enter an integer'

这篇关于如果用户在 Python/Tkinter 中输入字符串,如何告诉用户只使用整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆