Tuesday, 15 June 2010

tkinter - Python: I am working on a Restaurant tip calculator, I am trying to integrate radio buttons -



tkinter - Python: I am working on a Restaurant tip calculator, I am trying to integrate radio buttons -

i need know how implement selected radio button calculations. , help! i'm not positive problem is, quest comes "def selection" part. don't know there

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 addorigbill(self): label(self, text = "bill amount", font = self.headerfont).grid(columnspan = 1) self.txtbillamount = entry(self) self.txtbillamount.grid(row = 1, column = 1) self.txtbillamount.insert(0,"100.00") def addchooseone(self): label(self, text = "pick one! take % of tip or rate experience", font = self.headerfont).grid(row = 2, column = 1) def addperctip(self): label(self, text = "% of tip", font = self.headerfont).grid(row = 3, column = 0) self.radperctip1 = radiobutton(self, text = "15%", variable = self.percvar, value = .15, command = self.selected) self.radperctip2 = radiobutton(self, text = "17%", variable = self.percvar, value = .17, command = self.selected) self.radperctip3 = radiobutton(self, text = "20%", variable = self.percvar, value = .20, command = self.selected) self.radperctip1.grid(row = 4, column = 0) self.radperctip2.grid(row = 5, column = 0) self.radperctip3.grid(row = 6, column = 0) def selected(self): float(self.percvar.get()) def addratetip(self): label(self, text = "tip rating").grid(row = 3, column = 3) label(self, text = "1 beingness worst").grid(row = 4, column = 3) label(self, text = "10 beingness best").grid(row = 5, column = 3) label(self, text = "experience").grid(row = 6, column = 2) self.txtexperience = entry(self) self.txtexperience.grid(row = 6, column = 3) def addoutput(self): self.btncalc = button(self, text = "calculate tip") self.btncalc.grid(row = 7, columnspan = 2) self.btncalc["command"] = self.calculate label(self, text = "tip").grid(row = 8, column = 1) self.lbltip = label(self, bg = "#ffffff", anchor = "w", relief = "ridge") self.lbltip.grid(row = 8, column = 2, sticky = "we") label(self, text = "total bill").grid(row = 9, column = 1) self.lbltotalbill = label(self, bg = "#ffffff", anchor = "w", relief = "ridge") self.lbltotalbill.grid(row = 9, column = 2, sticky = "we") def calculate(self): bill = float(self.txtbillamount.get()) perctip = self.percvar ratetip = int(self.addratetip.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 wrong" def main(): app = app() app.mainloop() if __name__ == "__main__": main()

there no self.percvar in code. @wastl said, need initialize it.

to that, need utilize 1 of variable classes. since using float type, doublevar() best.

def addperctip(self): self.percvar = doublevar() #this line should added in method label(self, text = "% of tip", font = self.headerfont).grid(row = 3, column = 0) def selected(self): print (type(self.percvar.get())) #which float, without converting explicitly because of doublevar() print (self.percvar.get()) #this print click

python tkinter radio-button

No comments:

Post a Comment