0

how to convert the Entry string into numerical int or float values

#========================= Imports files and Widgets 
#===================================================================

from tkinter import *

#========================= Main Window Size 
main_window = Tk()
main_window.title("Entry Input Test")

width = 400
height = 200
main_window.geometry(str(width) + "x" + str(height) + "+600+520")
main_frame=Frame(main_window)

################ Varibles 

VarWidthECC = 20

################ Input Boxes 

how to convert the Entry input string into a int or float values

First_Value=Label(main_frame,text="Enter First Value ")
First_Value.grid(row=10,column=5, padx=10, sticky=E)
First_Value=(Entry(main_frame, width=VarWidthECC, justify='right'))
#First_Value=Input(main_frame, width=VarWidthECC, justify='right')
First_Value.grid(row=10,column=6, padx=10, pady=0)

Second_Value=Label(main_frame,text="Enter Second Value ")
Second_Value.grid(row=20,column=5, padx=10, sticky=E)
Second_Value=Entry(main_frame, width=VarWidthECC, justify = 'right')
#Second_Value=Input(main_frame, width=VarWidthECC, justify = 'right')
Second_Value.grid(row=20,column=6, padx=20, pady=10)

################ Comparison Function 

def Enter_Button():

    #global First_Value, Second_Value

    print("First Value is: " + First_Value.get())
    print("Second Value is: " + Second_Value.get())
    #value = int(First_Value)
    print(type(First_Value))

how to convert the Entry string into numerical int or float values

    #Multiply_test = int(First_Value) * 5  #this line of code is Error
    #How can I convert Entry string into int or float??

the Multiply test below works Multiply_test = 6 * 6 print("Multiply_test is: " + str(Multiply_test))

################ Run Button ####################################
button_enter = Button(main_frame, width=20, background="light grey", 
text=" Enter ", font="Verdana 10 bold", command=Enter_Button, bd=6, 
relief='raise')
button_enter.grid(row=100, column=6, padx=10, pady=5)

####################################################

main_frame.grid(row=0,column=0)       # Prints out Labels on Main Window
main_window.mainloop()                # Code to End Main Window (root)
Nouman
  • 6,947
  • 7
  • 32
  • 60
Dave
  • 1

2 Answers2

2

Using .get() method gives the input of the entry in the form of the string. You are trying to convert the tkinter entry to float/int but you forgot to use the get method.

Replace the Enter_Button() with this and try:

def Enter_Button():

    #global First_Value, Second_Value

    print("First Value is: " + First_Value.get())
    print("Second Value is: " + Second_Value.get())
    #value = int(First_Value)
    print(type(First_Value.get()))   #< Use get method as you used it above
    print(type(int(First_Value.get())))
    print(type(float(First_Value.get())))
Nouman
  • 6,947
  • 7
  • 32
  • 60
  • Thank you very much, your code works great. print(type(int(First_Value.get()))) print(type(float(First_Value.get()))) – Dave Aug 03 '19 at 23:18
0

.get() returned an str object , we already know python uses . for float identication.

if '.' in First_Value.get():
    # You got an Float
    return float(First_Value.get()) # Never reached to else for TYPE error
else :
    #You got an Integer
    return int(First_Value.get())

Actually this not an total soulotion, check this .

dsgdfg
  • 1,492
  • 11
  • 18