【问题标题】:not getting the float answer没有得到浮动答案
【发布时间】:2018-09-01 22:13:22
【问题描述】:
from tkinter import *
from datetime import datetime
import pickle
import os

##database will be with these mulas = dict{"name":["value","rate","left"]}

def _price_inputs():

    filename = "datas.pk"

    rupen = Tk()
    rupen.title("Montessori Management System")
    rupen.geometry("1600x800")
    rupen.configure(bg="black")

    framex = Frame(rupen, width=1600, bg="RoyalBlue4", height=100, relief=GROOVE).pack(side=TOP)

    # ==++++===========================title=============================

    #this is the variable for incoming
    names = StringVar()
    rates = IntVar()
    totals = IntVar()


    llb1 = Label(framex, font=("arial", 20, "italic"), bg="black", fg="green", text="STOCK MANAGEMENT",
                 relief=GROOVE).pack(side=TOP)

    now = datetime.now()
    hour = str(now.hour)
    d = str("\\")
    minute = str(now.minute)
    second = str(now.second)
    year = str(now.year)
    month = str(now.month)
    day = str(now.day)

    time =  "\t\t"+year + d + month + d + day + "\n\t\t" + hour + ":" + minute + ":"+second+"\n\n"
    showing = str("#") * 100 + "\n"

    def add_on():

        name = names.get()
        rate = float(rates.get())
        total = float(totals.get())
        with open(filename, "rb") as f:
            dic = pickle.load(f)

        dic[name] = [rate,total]

        with open(filename, "wb") as f:
            pickle.dump(dic, f)

        names.set("")
        rates.set("")
        totals.set("")
        """with open(filename, "rb") as f:
            dic = pickle.load(f)
            lbl.insert(END, "\n")
            lbl.insert(END, dic)
        print(dic)"""
        #_price_inputs()
        #add_fun()
        rupen.destroy()
        _price_inputs()

    def _sold():
        nam = names.get()
        rat = rates.get()
        total = totals.get()
        total = float(total)

        with open(filename, "rb") as f:
            dic = pickle.load(f)

        sold = dic[nam][1]
        dic[nam][1] = sold - rat
        with open(filename, "wb") as f:
            pickle.dump(dic, f)

        names.set("")
        rates.set("")
        totals.set("")
        rupen.destroy()
        _price_inputs()


    def quit():
        # show = "asjdfaskldjflsajdlfj"
        lbl.delete(0.0, END)
        rupen.destroy()
        # lbl.insert(END,"asjdfaskldjflsajdlfj")

    '''
            rate = str(rate)
            total = str(total)
            with open(filename, "rb") as f:
                dic = pickle.load(f)
                if os.path.getsize(filename) >= 1:
                    dic[name] = [rate,total]
                    lbl.insert(END, dic)'''

    lbl = Text(rupen, wrap=WORD, font=("arial", 16, "bold"), height=100, fg="red", width=100)
    lbl.pack(side=RIGHT)


    # show = "asjdfaskldjflsajdlfj"
    with open(filename, "rb") as f:
        ano = pickle.load(f)
    lbl.insert(END, time)

    for k, v in ano.items():
            lbl.insert(END, "\n")
            lbl.insert(END, k)
            lbl.insert(END, "--> ")
            lbl.insert(END, "\t")
            lbl.insert(END, v[0])
            lbl.insert(END, "\t")
            lbl.insert(END, v[1])
            lbl.insert(END, "\n")
    '''for k, v in dic.items():
        show = """{} -->   rate:- {}    Total:- {}  
                """.format(k,v[0],v[1])
    lbl.insert(END, show)
'''

    ####################ENTRY############################
    ent1 = Entry(rupen,font=("arial",16,"bold"),textvariable=names,bd=5,bg="black",fg="white")
    ent1.pack(side=BOTTOM)
    ent2 = Entry(rupen,font=("airal",16,"bold"),bd=5,bg="black",textvariable=rates,fg="white")
    ent2.pack(side=BOTTOM)
    ent3 = Entry(rupen,font=("arial",16,"bold"),bd=5,bg="black",textvariable=totals,fg="white")
    ent3.pack(side=BOTTOM)

####################BUTTONS#########################
    btn0 = Button(rupen,font=("arial",16,"bold"),bd=5,bg="black",text="sold",fg="white",command=_sold)
    btn0.pack(side=BOTTOM)
    btn = Button(rupen, font=("arial", 16, "bold"), bd=5, bg="black", fg="white", text="quit", command=quit,
                 relief=RAISED)
    btn.pack(side=BOTTOM)
    btn2 = Button(rupen, font=("arial", 16, "bold"), bd=5, bg="black", fg="white", text="Add", relief=RAISED,
                  command=add_on)
    btn2.pack(side=BOTTOM)

    def rupen_():
        rupen.destroy()



    #with open("filename.pk", "wb") as f:
        #pickle.dump(f, data)
     #   pass
    rupen.mainloop()

if __name__ == "__main__":
    _price_inputs()

我有这个 tkinter 代码,我可以在其中添加带有项目名称 rate 和 Total 的项目。单击添加或出售按钮时,我没有得到浮动答案。

假设我想添加 12.3 速率和总计 10.5 的 item1,在我单击添加按钮后,它只添加 12.0 和 10.0 之后的值。丢失了。

【问题讨论】:

标签: python python-3.x tkinter casting upcasting


【解决方案1】:

您不能将整数向上转换为浮点数,并且不希望丢失数据。

rate = float(rates.get())
total = float(totals.get())

上面的代码没有返回输入的浮点值。因为您首先将浮点值向下转换为整数,所以在. 之后丢失了数据。相反,从一开始就使用DoubleVar 而不是IntVar

rates = DoubleVar()
totals = DoubleVar()

【讨论】:

  • 感谢您的解决方案,但它无法正常工作......由于浮动,我遇到了计算错误
【解决方案2】:

您可以通过使用string formatting 来获得它。这个"%.2f" 表示值后两位小数。

替换这个

dic[name] = [rate, total]

有了这个

dic[name] = ["%.2f"%rate, "%.2f"%total]

你可以使用string formatting为浮点添加所有变量

"%.2f"

这个小例子向你演示如何使用string formattingInteger 转换为float 想要获得两位小数

from tkinter import *


def sum():
    two_decimal = float(e1.get())- float(e2.get())
    print(two_decimal) # this will print with only one decimal place
    print("%.2f"%two_decimal) # this will print with only two decimal places


root = Tk()

e1 = Entry(root)
e1.insert(0, 400)
e1.pack()

e2 = Entry(root)
e2.insert(0, 200)
e2.pack()

b = Button(root, text="Want to get float value", command=sum)
b.pack()

root.mainloop()

【讨论】:

  • 哦,只显示小数点后的两个数字我可以使用round函数
  • 我所做的不会在整数丢失后产生值,我已经尝试过我建议的更改
  • 你能告诉我如何在两天后向用户显示通知......即使程序可以在几天之间关闭......
  • 打开一个新问题
  • 我是有限的兄弟请帮助我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 2020-01-06
  • 2017-03-06
  • 2018-12-08
相关资源
最近更新 更多