python - Updating the Tkinter button error with code structure Explanation required -
so i'm teaching myself python, , developing simple gui restaurant menu. came across error i'm hoping explain, i've solved doing self.total_button option, rather putting options in parentheses (like did button above it), thing have changed , program runs no errors.
the line in total method self.total_button["text"] = "total £"+ str(self.cash_total) works when declare self.total_button way do, if declare each option in parentheses states nonetype' object not support item assignment.
does layout matter buttons in tkinter when updating them later in program?
#order up! #a simple gui program, presents simple restaurant menu. #it lists items, , prices. #let user select different items , show user total bill. tkinter import * class application(frame): """create gui application.""" def __init__(self, master): """initialises frame""" super(application, self).__init__(master) self.grid() self.cash_total = 0 self.total_list = [] self.create_widgets() def create_widgets(self): """creates widgets creates menu ordering systems""" #create instruction label label(self, text = "click desired buttons order something" ).grid(row = 0, column = 0, columnspan = 4, sticky = n) #create burger button button(self, text = "hamburger no bun", command = self.hamburger_no_bun ).grid(row = 1, column = 0, sticky = w) #creates total button #super weird bug have set out can use total method later. self.total_button = button(self) self.total_button["text"] = "total: £" self.total_button["command"] = self.total self.total_button.grid(row = 5, column = 5, sticky = w) #create text box show current order self.order_txt = text (self, width = 100, height = 8, wrap = word) self.order_txt.grid(row = 1, column = 5, sticky = w) def hamburger_no_bun(self): """creates hamburger tuple added list.""" self.hamburger_no_bun = ("hamburger no bun, £", 2.95) self.total_list.append(self.hamburger_no_bun) self.order_txt.insert(0.2, str(self.hamburger_no_bun)) def total(self): #the affected method """adds total amount due taken total_list""" in self.total_list: self.cash_total += i[1] print(self.cash_total) self.total_button["text"] = "total £"+ str(self.cash_total) self.cash_total = 0 #main root = tk() root.title("order up! - restaurant menu gui") app = application(root) root.mainloop()
you wrote code this
self.total_button = button(self, self, text="total: £", command=self.total).grid(row = 5, column = 5, sticky = w) you may suprised learn self.total_button holds value none. because holding return value of grid method of button not button reference itself.
later when try use self.total_button throw exception, because value none , none has no attribute "text".
to resolve issue must correctly capture reference button , split line creating , setting button 2 lines.
self.total_button = button(self, text="total: £", command=self.total) self.total_button.grid(row = 5, column = 5, sticky = w) now have correct reference button, usable later in total method.
Comments
Post a Comment