【问题标题】:Having trouble for calculating total amount of Items无法计算项目的总金额
【发布时间】:2020-12-17 08:45:17
【问题描述】:

我无法理解为什么我列表中的前 2 项中的某些项目没有被计算。我错过了什么?

代码:

from tkinter import *
root = Tk()
root.geometry('300x200')
root.title("Grade Calculator")

def exitProgram():
root.destroy()

def  calculate():
    totalcost = 0
    if CheckVar1.get() == 1:
        totalcost += 10
    if CheckVar2.get() == 1:
        totalcost += 2.50
    if CheckVar3.get() == 1:
        totalcost += 4
        label_2.config(text="Total cost: $"+str(totalcost))

CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()

label_1 = Label(root, text="Select from the menu: ",font= 

("粗体", 10)) label_1.place(x=20,y=10)

Checkbutton(root, text = "Premium Whapper ($10.0)", variable = CheckVar1, onvalue = 1, offvalue = 0).place(x=50,y=40)
Checkbutton(root, text = "Inca cola ($2.50)", variable = 
CheckVar2, onvalue = 1, offvalue = 0).place(x=50,y=65)
Checkbutton(root, text = "Smash potatoes ($4.0)", variable 
= CheckVar3, onvalue = 1, offvalue = 0).place(x=50,y=90)
label_2 = Label(root, text="Total cost: $0 ",font=("bold", 
10))
label_2.place(x=30,y=120)

Button(root, text='Calculate 
Cost',command=calculate).place(x=20,y=150)
Button(root, text='Quit',width="7", 
command=exitProgram).place(x=130,y=150)

root.mainloop()

【问题讨论】:

  • 你的输入是什么,预期的输出是什么,你得到的输出是什么?
  • 嗨,它显示了 3 个项目及其相应的价格。勾选时不计算第一项和第二项。
  • 好的,我试试
  • 不,不工作。

标签: python python-3.x tkinter pyzo


【解决方案1】:

只有在 CheckVar3 为真时才设置标签:

if CheckVar3.get() == 1:
    totalcost += 4
    label_2.config(text="Total cost: $"+str(totalcost))

由于您的缩进有问题,应该是:

if CheckVar3.get() == 1:
    totalcost += 4

label_2.config(text="Total cost: $"+str(totalcost))

【讨论】:

    【解决方案2】:

    您有大量的缩进错误和中断的控制流程! 无论如何,这是工作代码:

    from tkinter import *
    
    def exitProgram():
        root.destroy()
    
    def calculate():
        global label_2, totalcost
        if CheckVar1.get() == 1:
            totalcost += 10
        if CheckVar2.get() == 1:
            totalcost += 2.50
        if CheckVar3.get() == 1:
            totalcost += 4
        label_2.config(text="Total cost: $"+str(totalcost))
    
    totalcost = 0
    root = Tk()
    root.geometry('300x200')
    root.title("Grade Calculator")
    
    CheckVar1 = IntVar()
    CheckVar2 = IntVar()
    CheckVar3 = IntVar()
    
    label_1 = Label(root, text="Select from the menu: ",font=("bold", 10))
    label_1.place(x=20,y=10)
    
    Checkbutton(root, text = "Premium Whapper ($10.0)", variable = CheckVar1, onvalue = 1, offvalue = 0).place(x=50,y=40)
    Checkbutton(root, text = "Inca cola ($2.50)", variable =CheckVar2, onvalue = 1, offvalue = 0).place(x=50,y=65)
    Checkbutton(root, text = "Smash potatoes ($4.0)", variable= CheckVar3, onvalue = 1, offvalue = 0).place(x=50,y=90)
    label_2 = Label(root, text="Total cost: $0 ",font=("bold",10))
    label_2.place(x=30,y=120)
    
    Button(root, text='Calculate Cost',command=calculate).place(x=20,y=150)
    Button(root, text='Quit',width="7",command=exitProgram).place(x=130,y=150)
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 2016-12-06
      • 1970-01-01
      相关资源
      最近更新 更多