【问题标题】:Limit user input to numbers? Tkinter-Python [duplicate]将用户输入限制为数字? Tkinter-Python [重复]
【发布时间】:2015-01-28 18:36:43
【问题描述】:

我正在尝试在 python tkinter 中制作一个基本的计算器。我制作了一个输入框,用户可以在其中输入他的第一个数字。但是,如果有人输入的不是数字而是文本怎么办?我的问题是,如何使您只能在输入框中输入数字,或者如何忽略普通字母。

顺便说一下,我的半成品代码在这里:

from tkinter import *

okno = Tk()

okno.title("Kalkulačka")
okno.geometry("400x500")

firstLabel = Label(text="Vaše první číslo").place(x=25, y=25)
firstInput = Entry(text="Vaše první číslo").place(x=130, y=25, width=140)


buttonplus = Button(text="+").place(x=130, y=75)
buttonminus = Button(text="-").place(x=165, y=75)
buttonkrat = Button(text="・").place(x=197, y=75)
buttondeleno = Button(text=":").place(x=237, y=75)

【问题讨论】:

    标签: python input tkinter limit


    【解决方案1】:

    我个人会做的是在接受每个用户输入作为输入之前通过一个整数验证函数运行每个用户输入。像这样简单的事情:

    def is_int(x):
        try:
            x = int(x)
            return True
        except:
            return False
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 2020-08-24
      • 1970-01-01
      相关资源
      最近更新 更多