首页 > 解决方案 > 如何在金额前添加 $ 符号?

问题描述

我在下面展示的代码有一些 if 语句,它们产生不同的变量,这些变量都涉及金钱。当我运行代码时,我可以选择任何饮料的数量以及是否是外卖。如果是外卖,它会增加 5% 的成本,如果超过 3 杯饮料,它会从发票中扣除 10%。

谁能帮我+$在gui的输出区域添加一个外卖和-$折扣。

任何帮助,将不胜感激。

如果有人想查看整个代码,可以在这个 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()

标签: pythontkinter

解决方案


您可以在输入字段中插入默认值;这是一个小例子,tk.Entry你可以在你自己的项目中重用子类:

import tkinter as tk

class EntryBoxWithNegativeDollarSign(tk.Entry):
    def __init__(self, master, *args, **kwargs):
        self.master = master
        super().__init__(self.master, *args, **kwargs)
        self.default = '-$'
        self.insert(0, self.default)
        self.pack()

    def set_default(self):
        self.delete('0',tk.END)
        self.insert(0, self.default)

    def get(self):
        value = - float(super().get()[2:])
        self.set_default()
        print(value)

        return value

root = tk.Tk()
app = tk.Frame(root)
app.pack()
entry = EntryBoxWithNegativeDollarSign(app)
tk.Button(app, text='get value', command=entry.get).pack()

root.mainloop()

推荐阅读