1

I am trying to make a basic calculator in python tkinter. I made an entry box that user types his first number into. But then, what if someone enters in something other than numbers, but text? My question is, how to make that you can put only number to entry box, or how it can ignore normal letters.

By the way, my half-done code is here:

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)
Sonicpeta
  • 23
  • 1
  • 5

1 Answers1

2

What I would do personally is run every user input through an integer verifying function before accepting it as input. Something simple like this:

def is_int(x):
    try:
        x = int(x)
        return True
    except:
        return False
oxrock
  • 643
  • 5
  • 12