python - 为什么我的代码不生成小计、增值税和总计值?
问题描述
我一直按照指南一步一步地创建 Tkinter 管理系统,但是,当我添加甜蜜数量并单击总计按钮以创建小计、增值税和总浮点输出时,我的代码仍然没有输出值。我是编码新手,无法在线或教程中找到解决方案。谢谢,莫。
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% 的税相加是一样的
推荐阅读
- python - python生成器:yield和yield from
- java - Gradle 任务间歇性失败
- html - 什么决定了一个块的高度,当它的高度:自动;
- python - Django-Allauth - 自定义用户模型和自定义注册表单的错误
- sql - 谷歌大查询:在有多个company_id记录的表中,如何只为表中出现最早的每个公司id输出一行?
- javascript - 如何将对象数组转换为对象列表
- javascript - 想问一下google脚本是如何工作的?
- scala - 使用猫嵌套映射一系列期货?
- scala - 从 Scala 期货中获取价值
- node.js - 来自 URL 源的图像与 Express 服务器中的本地文件