[英]Why doesn't my code produce the Subtotal, VAT and Total values?
我一直按照指南一步一步地創建一個 Tkinter 管理系統,但是,當我添加甜蜜數量並單擊總計按鈕創建小計、增值稅和總浮動 output 時,我的代碼仍然沒有 output 值我是編碼新手,無法在線或教程中找到解決方案。 謝謝,莫。
from tkinter import*
import random
import time
def Ref():
x = random.randint(1234, 434354)
randomRef = str(x)
rand.set(randomRef)
CoG = float(galaxy.get())
CoK = float(kinder.get())
CoB = float(bounty.get())
CoT = float(twix.get())
Cost_Galaxy = CoG * 1.99
Cost_Kinder = CoK * 1.49
Cost_Bounty = CoB * 1.29
Cost_Twix = CoT * 0.99
cost_subtotal = "£", str('%.2f' % (Cost_Bounty + Cost_Galaxy + Cost_Kinder + Cost_Twix))
cost_tax = "£", str((Cost_Bounty + Cost_Galaxy + Cost_Kinder + Cost_Twix) * 0.2)
cost_total = "£", (Cost_Bounty + Cost_Galaxy + Cost_Kinder + Cost_Twix + cost_tax)
subtotal.set(cost_subtotal)
vat.set(cost_tax)
total.set(cost_total)
def Quit():
root.destroy()
def Reset():
rand.set("")
galaxy.set("")
kinder.set("")
bounty.set("")
twix.set("")
total.set("")
subtotal.set("")
vat.set("")
root = Tk()
root.geometry("1600x800+0+0")
root.title("Sea Front Sweet & Candy Shop System")
Tops = Frame(root, width = 1600, height = 50, bg = "light blue", relief=SUNKEN)
Tops.pack(side=TOP)
Frame_1 = Frame(root, width = 800, height = 700, relief=SUNKEN)
Frame_1.pack(side=LEFT)
#---------------------------Time---------------------------
localtime = time.asctime(time.localtime(time.time()))
#---------------------------Info---------------------------
lblInfo = Label(Tops, font = ('arial', 50, 'bold'), text = 'Sea Front Sweet & Candy Shop System', fg = 'navy', bd = 10, anchor = 'w')
lblInfo.grid(row = 0, column = 0)
lblInfo = Label(Tops, font = ('arial', 20, 'bold'), text = localtime, fg = 'navy', bd = 10, anchor = 'w')
lblInfo.grid(row = 1, column = 0)
#---------------------------Sweets---------------------------
rand = StringVar()
galaxy = StringVar()
kinder = StringVar()
bounty = StringVar()
twix = StringVar()
total = StringVar()
subtotal = StringVar()
vat = StringVar()
lblReference = Label(Frame_1, font = ('arial', 16, 'bold'), text = 'Reference', bd = 10, anchor = 'w')
lblReference.grid(row = 0, column = 0)
txtReference = Entry(Frame_1, font = ('arial', 16, 'bold'), textvariable = rand, bd = 10, bg = 'light blue', justify = 'right')
txtReference.grid(row = 0, column = 1)
lblGalaxy = Label(Frame_1, font = ('arial', 16, 'bold'), text = 'Galaxy', bd = 10, anchor = 'w')
lblGalaxy.grid(row = 1, column = 0)
txtGalaxy = Entry(Frame_1, font = ('arial', 16, 'bold'), textvariable = galaxy, bd = 10, bg = 'light blue', justify = 'right')
txtGalaxy.grid(row = 1, column = 1)
lblKinder = Label(Frame_1, font = ('arial', 16, 'bold'), text = 'Kinder', bd = 10, anchor = 'w')
lblKinder.grid(row = 2, column = 0)
txtKinder = Entry(Frame_1, font = ('arial', 16, 'bold'), textvariable = kinder, bd = 10, bg = 'light blue', justify = 'right')
txtKinder.grid(row = 2, column = 1)
lblBounty = Label(Frame_1, font = ('arial', 16, 'bold'), text = 'Bounty', bd = 10, anchor = 'w')
lblBounty.grid(row = 4, column = 0)
txtBounty = Entry(Frame_1, font = ('arial', 16, 'bold'), textvariable = bounty, bd = 10, bg = 'light blue', justify = 'right')
txtBounty.grid(row = 4, column = 1)
lblTwix = Label(Frame_1, font = ('arial', 16, 'bold'), text = 'Twix', bd = 10, anchor = 'w')
lblTwix.grid(row = 5, column = 0)
txtTwix = Entry(Frame_1, font = ('arial', 16, 'bold'), textvariable = twix, bd = 10, bg = 'light blue', justify = 'right')
txtTwix.grid(row = 5, column = 1)
#--------------------Pricing------------------
lblSubtotal = Label(Frame_1, font = ('arial', 16, 'bold'), text = 'Subtotal', bd = 10, anchor = 'w')
lblSubtotal.grid(row = 1, column = 2)
txtSubtotal = Entry(Frame_1, font = ('arial', 16, 'bold'), textvariable = subtotal, bd = 10, bg = '#ffffff', justify = 'right')
txtSubtotal.grid(row = 1, column = 3)
lblVat = Label(Frame_1, font = ('arial', 16, 'bold'), text = 'VAT', bd = 10, anchor = 'w')
lblVat.grid(row = 2, column = 2)
txtVat = Entry(Frame_1, font = ('arial', 16, 'bold'), textvariable = vat, bd = 10, bg = '#ffffff', justify = 'right')
txtVat.grid(row = 2, column = 3)
lblTotal = Label(Frame_1, font = ('arial', 16, 'bold'), text = 'Total', bd = 10, anchor = 'w')
lblTotal.grid(row = 4, column = 2)
txtTotal = Entry(Frame_1, font = ('arial', 16, 'bold'), textvariable = total, bd = 10, bg = '#ffffff', justify = 'right')
txtTotal.grid(row = 4, column = 3)
#-------------------Buttons------------------
btnTotal = Button(Frame_1, padx = 16, pady = 8, bd = 10, fg = 'black', font = ('arial', 16, 'bold'), text = 'Total',
bg = 'light blue', command = Ref).grid(row = 7, column = 1)
btnReset = Button(Frame_1, padx = 16, pady = 8, bd = 10, fg = 'black', font = ('arial', 16, 'bold'), text = 'Reset',
bg = 'light blue', command = Reset).grid(row = 7, column = 2)
btnQuit = Button(Frame_1, padx = 16, pady = 8, bd = 10, fg = 'black', font = ('arial', 16, 'bold'), text = 'Quit',
bg = 'light blue', command = Quit).grid(row = 7, column = 3)
root.mainloop()
問題是在cost_total
中,您使用cost_tax
來計算總值,但cost_tax
是一個字符串而不是浮點數。 如果您通過以下方式更改此行,您的程序將起作用:
cost_total = "£", str((Cost_Bounty + Cost_Galaxy + Cost_Kinder + Cost_Twix)*1.2)
這與將小計與小計的 20% 的稅相加是一樣的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.