我需要帮助,了解如何使用我所做的复选框选中时关闭程序的一部分,并在复选框关闭时关闭另一部分。我的想法是,当复选框打开时,我希望打开addPercTip(self)部分并关闭addRateTip,并在复选框关闭时反之亦然。 PercTip关闭和RateTip。我现在的问题是,在我的计算中,它试图从两个部分获取信息,所以其中一个需要关闭。任何帮助将非常感激!使复选框打开和关闭一些东西?
from Tkinter import *
class App(Tk):
def __init__(self):
Tk.__init__(self)
self.headerFont = ("Times", "16", "italic")
self.title("Restaurant Tipper")
self.addOrigBill()
self.addChooseOne()
self.addPercTip()
self.addRateTip()
self.addOutput()
def addChooseOne(self):
Label(self, text = "Check ON for % check OFF for rating!",
font = self.headerFont).grid(row = 2, column = 1)
self.checkVar = IntVar()
self.chkCheck = Checkbutton(self, variable = self.checkVar)
self.chkCheck.grid(row = 3, column = 1)
def calculate(self):
bill = float(self.txtBillAmount.get())
percTip = self.percVar
rateTip = int(self.scrScale.get())
tip = bill * percTip
self.lblTip["text"] = "%.2f" % tip
totalBill = tip + bill
self.lblTotalBill["text"] = "%.2f" % totalBill
if rateTip <= 2:
percTip = .10
elif 3 <= rateTip <= 4:
percTip = .12
elif 5 <= rateTip <= 6:
percTip = .15
elif 7 <= rateTip <= 8:
percTip = .17
elif 9 <= rateTip <= 10:
percTip = .20
else:
self.lblTotalBill["text"] = "Something is wrong"
def main():
app = App()
app.mainloop()
if __name__ == "__main__":
main()
编辑你的例子只是负责检测复选框更改状态(或者它的声明,如果你没有),然后显示最终输出计算将有助于提供一个很好的答案。 – aruisdante
一般来说,如果self.checkVar else self.calcRate()''calcRate()''返回基于您的评分等级的提示百分比,则可以使用''percTip = self.percVar轻松解决此问题。 – aruisdante
我想我不确定我需要在哪里添加该内容。对不起,我只是带着我的第一堂课,而我正试图超越这个项目。 – BondyeLwa