2 回答
TA贡献2080条经验 获得超4个赞
您正在做的是将转换为字符串的数字连接到 self.message 的每个 if 语句中。
例如。如果您输入前两个 if 语句,您首先会得到“您的总费用 = 30 美元”,然后是“您的总费用 = 3020 美元”
相反,您应该在最后将 chargeTotal 添加到您的 self.message 中,仅一次:
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
TA贡献1898条经验 获得超8个赞
您需要删除self.message += str(chargeTotal)每个if语句中的 ,然后将其放在下面的代码中。希望它有帮助。
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
添加回答
举报