简体   繁体   English

无法从输入字段获取整数值

[英]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.然后它会显示顶部定义的 add() 函数中给出的输入字段。 After inputting all the information, the user clicks submit.输入所有信息后,用户点击提交。 The program then sends a query to update a mysql database with the given information.然后该程序发送一个查询以使用给定的信息更新 mysql 数据库。

My problem is that I need "stock_unit_price" as float and "stocks_bought" as int so that the query is successful.我的问题是我需要“stock_unit_price”作为浮点数和“stocks_bought”作为int,以便查询成功。 But it appears that the entry field is giving a string value.但似乎输入字段给出了一个字符串值。

I tried converting the string into int like:我尝试将字符串转换为 int ,例如:

int('5.0')

And even tried like:甚至尝试过:

int(float('5.0'))

But nothing works.但没有任何作用。 Pls help请帮忙

You are using it wrongly .You cant get and assign at same time.您使用错误。您不能同时获取和分配。 I will suggest you to use StringVar .我会建议你使用StringVar Please check the snippet请检查代码段

字符串变量

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.或者,如果你想按照你的方法去,你必须存储你的 get 值,它被转换为另一个变量中的整数,然后你可以使用它。

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这也将产生相同的整数输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM