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代码,我可以添加项目名称率和总计的项目。点击添加或售出按钮时,我没有得到浮动答案。
假设我要添加具有12.3速率和总计10.5的item1,并且在我单击添加按钮之后,它仅添加12.0和10.0之后的值。失去了。
答案 0 :(得分:0)
您不能将整数转换为浮点数,也不会丢失数据。
rate = float(rates.get())
total = float(totals.get())
上面的代码片段没有输入返回的浮点值。因为您首先将浮点值向下转换为整数,所以在.
之后丢失数据。相反,从一开始就使用DoubleVar
而不是IntVar
:
rates = DoubleVar()
totals = DoubleVar()
答案 1 :(得分:0)
您可以使用string formatting
来解决这个问题。此"%.2f"
表示值后的两位小数。
替换此
dic[name] = [rate, total]
用这个
dic[name] = ["%.2f"%rate, "%.2f"%total]
您可以使用string formatting
"%.2f"
这个小例子向您展示如何使用string formatting
使用时将Integer
转换为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()