限制用户输入数字?TkinterPython公司

2024-10-17 12:34:55 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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)

Tags: text用户文本字母placebutton数字计算器
1条回答
网友
1楼 · 发布于 2024-10-17 12:34:55

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

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

相关问题 更多 >