Sumathi Iyengar
Sumathi Iyengar

Reputation: 88

Not able to get integer value from entry field

I was trying to create a stock managing program as follows:

import mysql.connector as mc
from tkinter import*
import time
from datetime import date  
mycon=mc.connect(host="localhost",user="root",passwd="1234",database="stock_manager")
cursor=mycon.cursor()

global stock_name
global stock_unit_price
global stocks_bought


def add():
    global stock_name
    global stock_unit_price
    global stocks_bought

stock_name_label=Label(root,text="Enter Stock Name")

stock_name_entry=Entry(root,width=50)

stock_unit_price_label=Label(root,text="Enter unit price of stock")

stock_unit_price_entry=Entry(root,width=50)

stocks_bought_label=Label(root,text="Enter number of stocks bought: ")

stocks_bought_entry=Entry(root,width=50)

stock_name=stock_name_entry.get()

stock_unit_price=int(float(stock_unit_price_entry.get()))

stocks_bought=int(stocks_bought_entry.get())


stock_name_label.grid(row=2,column=0)

stock_name_entry.grid(row=3,column=0)

stock_unit_price_label.grid(row=4,column=0)

stock_unit_price_entry.grid(row=5,column=0)

stocks_bought_label.grid(row=6,column=0)

stocks_bought_entry.grid(row=7,column=0)


submit_stock_button=Button(root,text="Submit",command=submit)

submit_stock_button.grid(row=8,column=1)

def submit():
    global stock_name
    global stock_unit_price
    global stocks_bought

submitted_label=Label(root,text="Submitted!")
total_investment=(stock_unit_price)*(stocks_bought)
date=date.today()
cursor.execute("insert into 
all_stocks values(%s,%s,%s,%s,%s)"%(stock_name,stock_unit_price,stocks_bought,total_investment,date))
submitted_label.grid(row=9,column=1)



root=Tk()
title_label=Label(root,text="All Investments") 
add_stock_button=Button(root,text="Add Stock",command=add)
title_label.grid(row=0,column=1) 
add_stock_button.grid(row=1,column=0)
root.mainloop()

So what this program is supposed to do is let the user add a stock by clicking the button "add stock". It then reveals the entry fields as given in the add() function defined at the top. After inputting all the information, the user clicks submit. The program then sends a query to update a mysql database with the given information.

My problem is that I need "stock_unit_price" as float and "stocks_bought" as int so that the query is successful. But it appears that the entry field is giving a string value.

I tried converting the string into int like:

int('5.0')

And even tried like:

int(float('5.0'))

But nothing works. Pls help

Upvotes: 0

Views: 70

Answers (1)

Karthik
Karthik

Reputation: 2431

You are using it wrongly .You cant get and assign at same time. I will suggest you to use StringVar . Please check the snippet

Stringvar

import tkinter as tk   
root=tk.Tk() 
root.geometry("300x100") 

sk=tk.StringVar() 
sb=tk.StringVar() 

def Add():  
    stock_entry=int(stock_unit_price_entry.get()) 
    stock_bought=int(stocks_bought_entry.get())
    print(stock_entry+stock_bought)   
    sk.set("") 
    sb.set("") 
      

stock_name_label = tk.Label(root,text = 'Enter unit price of stock') 
stock_unit_price_entry = tk.Entry(root,textvariable = sk) 

stocks_bought_label = tk.Label(root,text = 'Enter number of stocks bought:', ) 
stocks_bought_entry=tk.Entry(root, textvariable = sb) 

submit_stock_button=tk.Button(root,text = 'Submit', command = Add) 
   
stock_name_label.grid(row=0,column=0) 
stock_unit_price_entry.grid(row=0,column=1) 
stocks_bought_label.grid(row=1,column=0) 
stocks_bought_entry.grid(row=1,column=1) 
submit_stock_button.grid(row=2,column=1) 
   
root.mainloop()

You will get integer as output

Or if you want to go by your method, you have to store your get value which is converted into integer in another variable and then you can use it.

stock_unit_price_entry=Entry(root,width=50)
stocks_bought_entry=Entry(root,width=50)
stock_unit=int(stock_unit_price_entry.get())
stock_bought=int(stocks_bought_entry.get())
print(stock_unit)
print(stock_bought)

This will also produce same integer output

Upvotes: 1

Related Questions