-1
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()

I have this tkinter code where i can add the items with the item name rate and Total. I am not getting the float answer while clicking add or sold button.

Suppose i want to add the item1 with 12.3 rate and Total 10.5 and after i click the add button it only adds 12.0 and 10.0 the value after the . is lost.

Nae
  • 14,209
  • 7
  • 52
  • 79

2 Answers2

0

You can't upcast an integer to a float and not expect to lose data.

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

The code piece above doesn't get the float values entered back. Because you downcast float values to integers first, losing the data after .. Instead, use DoubleVar as opposed to IntVar from the very beginning:

rates = DoubleVar()
totals = DoubleVar()
Nae
  • 14,209
  • 7
  • 52
  • 79
  • Thank you for the solution but its not working as it should have .....I am getting calculation mistakes due to the float – HEalik Tamu Mar 23 '18 at 09:44
0

You can get that by using string formatting. This "%.2f" means two decimal places after the value.

Replace this

dic[name] = [rate, total]

with this

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

all you can add all variable for the float with the string formatting

"%.2f"

This small example to demonstrate to you how to use string formatting when use convert an Integer to float want to get two decimal places

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()
AD WAN
  • 1,414
  • 2
  • 15
  • 28