Home > Back-end >  Turning String into Integer inside tkinter Entry
Turning String into Integer inside tkinter Entry

Time:11-07

I am currently experimenting with tkinter in python, but I couldnt figure out how to turn the string that is written inside an Entry to an integer.

from tkinter import *

window = Tk()
erstertext = Label(window, text="Hi! You have to enter your information for this program.")
erstertext.pack()

agetext = Label(window, text="Age:")
agetext.pack()
ageinput = Entry(window,)
ageinput.pack()
nametext = Label(window, text="First Name:")
nametext.pack()
nameinput = Entry(window,)
nameinput.pack()

x = ageinput.get()


def callback():
     if x >= 18:
         Text1 = Label(window, text="Hi "   nameinput.get()   " , you are "   ageinput.get()   "or older")
         Text1.pack()

     else:
        Text2 = Label(window, text="Hi "   nameinput.get()   " ur younger than 18")
        Text2.pack()

button1 = Button(window, text="Done",command=callback)
button1.pack()

window.mainloop()

The relevant part:

from tkinter import * 

agetext = Label(window, text="Age:")
agetext.pack()
ageinput = Entry(window,)
ageinput.pack()

x = ageinput.get()


def callback():
     if x >= 18:
         Text1 = Label(window, text="Hi "   nameinput.get()   " , you are "   ageinput.get()   "or older")
         Text1.pack()

I currently use Python 3.9

I tried turning x into an integer in the callback section

def callback():
     if int(x) >= 18:
         Text1 = Label(window, text="Hi "   nameinput.get()   " , you are "   ageinput.get()   "or older")
         Text1.pack()

still didnt work. Then I tried putting the ageinput.get directly into a int

def callback():
     if int(ageinput.get) >= 18:
         Text1 = Label(window, text="Hi "   nameinput.get()   " , you are "   ageinput.get()   "or older")
         Text1.pack()

and it doesnt work either.

CodePudding user response:

Currently your program gets the age input before the window is even shown on the screen.

The variable x, is only retrieved once and remains static for the duration of the program.

Move the line where you get the age input inside of the callback... for example:

from tkinter import *

window = Tk()
erstertext = Label(window, text="Hi! You have to enter your information for this program.")
erstertext.pack()

agetext = Label(window, text="Age:")
agetext.pack()
ageinput = Entry(window,)
ageinput.pack()
nametext = Label(window, text="First Name:")
nametext.pack()
nameinput = Entry(window,)
nameinput.pack()

def callback():
     x = ageinput.get()  # this grabs the input text at the time the button is pressed
     if x.isdigit() and int(x) >= 18:
         Text1 = Label(window, text="Hi "   nameinput.get()   " , you are "   ageinput.get()   "or older")
         Text1.pack()

     else:
        Text2 = Label(window, text="Hi "   nameinput.get()   " ur younger than 18")
        Text2.pack()

button1 = Button(window, text="Done",command=callback)
button1.pack()

window.mainloop()

CodePudding user response:

Does this help. In line 16 I replaced this: x = ageinput.get() to y = 18

Here is code:

from tkinter import *

window = Tk()
erstertext = Label(window, text="Hi! You have to enter your information for this program.")
erstertext.pack()

agetext = Label(window, text="Age:")
agetext.pack()
ageinput = Entry(window,)
ageinput.pack()
nametext = Label(window, text="First Name:")
nametext.pack()
nameinput = Entry(window,)
nameinput.pack()

y = 18

def callback():
     if int(ageinput.get()) >= y:
         Text1 = Label(window, text="Hi "   nameinput.get()   " , you are "   ageinput.get()   "or older")
         Text1.pack()

     else:
        Text2 = Label(window, text="Hi "   nameinput.get()   " ur younger than 18")
        Text2.pack()

button1 = Button(window, text="Done",command=callback)
button1.pack()

window.mainloop()

Result before:

enter image description here

Result after:

enter image description here

Result less age:

enter image description here

  • Related