如何在金额前添加$符号?

时间:2018-08-15 10:56:46

标签: python tkinter

我在下面显示的代码中有一些if语句,它们使不同的变量都涉及金钱。当我运行代码时,我可以选择多少种饮料以及是否外卖。 如果是外卖,则会增加5%的费用,如果饮料多于3种,则会使发票减少10%。

谁能帮助我在gui的输出区域中添加一个+$作为外卖,并为折扣添加一个-$

任何帮助将不胜感激。

如果有人想查看整个代码,则可以在this gist上看到它

import tkinter as tk
import time
import random

root = tk.Tk()
root.geometry("1600x8000")
root.title("Cafe au Lait")

tops = tk.Frame(root, width=1600, relief="sunken")
tops.pack(side="top")
f1 = tk.Frame(root, width=800, height=700, relief="sunken")
f1.pack(side="left")

latte = tk.DoubleVar()
double_expresso = tk.DoubleVar()
iced = tk.DoubleVar()
cost = tk.DoubleVar()
cappacuino = tk.DoubleVar()
expresso = tk.DoubleVar()
flat_white = tk.DoubleVar()
total = tk.DoubleVar()
takeaway_var = tk.StringVar()
rand = tk.IntVar()
discount = tk.DoubleVar()
takeaway = tk.DoubleVar()

def ref(even=None):
    x=random.randint(10000,99999)
    randomRef=str(x)
    rand.set(randomRef)

    global total, cost
    if latte.get() == "":
        col = 0
    else:
        col = latte.get() 
    if iced.get() == "":
        coi = 0
    else:
       coi = iced.get()
    if flat_white.get() == "":
        cofw = 0
    else:
        cofw = flat_white.get()
    if double_expresso.get() == "":
        code = 0
    else:
        code = double_expresso.get()
    if cappacuino.get() == "":
        coc = 0
    else:
        coc = cappacuino.get()
    if expresso.get() == "":
        coe = 0
    else:
        coe = expresso.get()

    costoflatte = col * 3.5
    costoficed = coi * 2.5
    costofflat_white = cofw * 3.75
    costofdouble_expresso = code * 4.25
    costofcappacuino = coc * 3.75
    costofexpresso = coe * 3

    total.set('{}'.format(cost.get() + costoflatte + costoficed + costofflat_white + costofdouble_expresso + costofcappacuino + costofexpresso))
    cost.set('{}'.format(cost.get() + costoflatte + costoficed + costofflat_white + costofdouble_expresso + costofcappacuino + costofexpresso))

    if txt_takeaway.get() in ['Yes', 'yes', 'y', 'Y']:
        w = total.get()
        takeaway.set(w * 0.05)

    if txt_takeaway.get() in ['Yes', 'yes', 'y', 'Y']:
        x = total.get()
        total.set((x * 0.05) + x)

    if (coc + col + coi + cofw + code + coe) >=3:
        z = total.get()
        discount.set(z * 0.1)

    if (coc + col + coi + cofw + code + coe) >=3:
        y = total.get()
        total.set(y * 0.9)

上面的代码将它们全部设置好,以便下面的代码正确输出。它需要运行。下面的前2个按钮是需要添加+ $和-$的按钮。

lbl_takeaway= tk.Label(f1, font=('arial', 16, 'bold'),text="Takeaway",bd=16,anchor="w").grid(row=2, column=2)
txt_takeaway=tk.Entry(f1, font=('arial',16,'bold'),textvariable=takeaway,bd=10,insertwidth=4,bg="powder blue",justify='right')
txt_takeaway.grid(row=2,column=3)

lbl_discount= tk.Label(f1, font=('arial', 16, 'bold'),text="Discount",bd=16,anchor="w").grid(row=3, column=2)
txt_discount=tk.Entry(f1, font=('arial',16,'bold'),textvariable=discount,bd=10,insertwidth=4,bg="powder blue",justify='right')
txt_discount.grid(row=3,column=3)

tk.Label(f1, font=('arial', 16, 'bold'),text="Order Number",bd=16,anchor="w").grid(row=0, column=2)
txt_order=tk.Entry(f1, font=('arial',16,'bold'),textvariable=rand,bd=10,insertwidth=4,bg="powder blue",justify='right')
txt_order.grid(row=0,column=3)

tk.Label(f1, font=('arial', 16, 'bold'), text="Takeaway", bd=16, anchor="w").grid(row=6, column=0)
txt_takeaway = tk.Entry(f1, font=('arial',16,'bold'),textvariable=takeaway_var, bd=10, insertwidth=4, bg="powder blue", justify='right')
txt_takeaway.grid(row=6, column=1)

tk.Label(f1, font=('arial', 16, 'bold'), text="Cost of Order", bd=16, anchor="w").grid(row=1, column=2)
txt_cost = tk.Entry(f1, font=('arial',16,'bold'), textvariable=cost,bd=10, insertwidth=4, bg="powder blue", justify='right')
txt_cost.grid(row=1, column=3)

tk.Label(f1, font=('arial', 16, 'bold'), text="Total Cost", bd=16, anchor="w").grid(row=5, column=2)
txt_totalcost = tk.Entry(f1, font=('arial',16,'bold'), textvariable=total, bd=10, insertwidth=4, bg="powder blue", justify='right')
txt_totalcost.grid(row=5, column=3)

tk.Label(f1, font=('arial', 16, 'bold'),text="Latte",bd=16,anchor="w").grid(row=1, column=0)
txt_latte=tk.Entry(f1, font=('arial',16,'bold'),textvariable=latte,bd=10,insertwidth=4,bg="powder blue",justify='right')
txt_latte.grid(row=1,column=1)

btnTotal=tk.Button(f1,padx=16,pady=8,bd=16,fg="black",font=('arial',16,'bold'),width=10,text="Total",bg="powder blue",command=ref).grid(row=7,column=1)

root.bind("<Return>", ref)

root.mainloop()

1 个答案:

答案 0 :(得分:2)

您可以在输入字段中插入默认值。这是一个子类if(file.open(QIODevice::ReadOnly)){ QJsonDocument doc = QJsonDocument::fromJson(file.readAll()); QJsonObject root = doc.object(); QJsonArray entries = root["entry"].toArray(); for(int i=0; i < entries.count(); i++){ QJsonObject obj = entries[i].toObject(); QJsonObject objs = obj["resource"].toObject(); if(objs.contains("gender")){ objs["gender"]= "female"; obj["resource"] = objs; entries[i] = obj; } } root["entry"] = entries; doc.setObject(root); // another process } 的小示例,您可以在自己的项目中重用:

tk.Entry