[Tkinter] AttributeError: 'App' object has no attribute 'set_text' - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] AttributeError: 'App' object has no attribute 'set_text' (/thread-10961.html) |
AttributeError: 'App' object has no attribute 'set_text' - Sahil1313 - Jun-15-2018 I am creating a python tkinter gui application.When i run my code the gui works fine but the buttons are not working properly.Please see the code below: from tkinter import * class App: def __init__(self,master): #Variables account_number = IntVar().set("") pin_number = StringVar() #Frames Main = Frame(master) Main.pack() Top = Frame(root, bd=2, relief=RIDGE,borderwidth=0,highlightthickness=0) Top.pack(side=TOP, fill=X,pady=36) Form = Frame(root,height=200) Form.pack(side=TOP) #Labels self.lbl_title = Label(Top,text = "FedUni Banking",font=("Arial",32)) self.lbl_title.pack() self.lbl_account = Label(Form,text = "Account Number / PIN",font=("Arial",11)) self.lbl_account.grid(row=0,sticky=E) #Textboxes self.tb_account = Entry(Form,width=13,textvariable=account_number,font=("Arial",15)) self.tb_account.grid(row=0,column=1,ipady=18) self.tb_pin = Entry(Form,width=13,show="*",textvariable=pin_number,font=("Arial",15)) self.tb_pin.grid(row = 0,column = 2,ipady=18) #Buttons self.btn_1 = Button(Form,text="1",width=20,height=7,command = lambda:self.set_text("1")) self.btn_1.grid(row=1,column=0) self.btn_2 = Button(Form,text="2",width=20,height=7,command = lambda:self.set_text("2")) self.btn_2.grid(row=1,column=1) self.btn_3 = Button(Form,text="3",width=20,height=7,command = lambda:self.set_text("3")) self.btn_3.grid(row=1,column=2) self.btn_4 = Button(Form,text="4",width=20,height=7,command = lambda:self.set_text("4")) self.btn_4.grid(row=2,column=0) self.btn_5 = Button(Form,text="5",width=20,height=7,command = lambda:self.set_text("5")) self.btn_5.grid(row=2,column=1) self.btn_6 = Button(Form,text="6",width=20,height=7,command = lambda:self.set_text("6")) self.btn_6.grid(row=2,column=2) self.btn_7 = Button(Form,text="7",width=20,height=7,command = lambda:self.set_text("7")) self.btn_7.grid(row=3,column=0) self.btn_8 = Button(Form,text="8",width=20,height=7,command = lambda:self.set_text("8")) self.btn_8.grid(row=3,column=1) self.btn_9 = Button(Form,text="9",width=20,height=7,command = lambda:self.set_text("9")) self.btn_9.grid(row=3,column=2) self.btn_cancel = Button(Form,text="Cancel/Clear",bg="red",width=20,height=7,command = lambda:clear()) self.btn_cancel.grid(row=4,column=0) self.btn_0 = Button(Form,text="0",width=20,height=7,command = lambda:self.set_text("0")) self.btn_0.grid(row=4,column=1) self.btn_login = Button(Form,text="Login",bg="green",width=20,height=7,command = lambda:login()) self.btn_login.grid(row=4,column=2) #Functions def set_text(e,text): widget = e.focus_get() if widget in e.entries: widget.insert(0, text) def clear(): self.tb_pin.delete(0,END) root = Tk() root.title("FedUni Banking") root.geometry("440x640") app = App(root) root.mainloop() RE: AttributeError: 'App' object has no attribute 'set_text' - Larz60+ - Jun-15-2018 use: def set_text(self, e,text):and def clear(self) RE: AttributeError: 'App' object has no attribute 'set_text' - woooee - Jun-15-2018 And the function def is indented one level further than it should be. Your functions are declared under the init function and so are garbage collected when init exits, (after the buttons are first created). Also, use partial so send parameters to the function. I don't know what "e" is supposed to be because you never declare it anywhere. The following code assumes that you want to delete the text from the button when you select it, so the following code demonstrates how to do that. from tkinter import * from functools import partial class App: def __init__(self,master): #Variables account_number = IntVar().set("") pin_number = StringVar() #Frames Main = Frame(master) Main.pack() Top = Frame(master, bd=2, relief=RIDGE,borderwidth=0,highlightthickness=0) Top.pack(side=TOP, fill=X,pady=36) Form = Frame(master,height=200) Form.pack(side=TOP) #Labels self.lbl_title = Label(Top,text = "FedUni Banking",font=("Arial",32)) self.lbl_title.pack() self.lbl_account = Label(Form,text = "Account Number / PIN",font=("Arial",11)) self.lbl_account.grid(row=0,sticky=E) #Textboxes self.tb_account = Entry(Form,width=13,textvariable=account_number,font=("Arial",15)) self.tb_account.grid(row=0,column=1,ipady=18) self.tb_pin = Entry(Form,width=13,show="*",textvariable=pin_number,font=("Arial",15)) self.tb_pin.grid(row = 0,column = 2,ipady=18) #Buttons this_row=1 this_column=0 ## store button instances in a dictionary instead of separate variables self.btn_dict={} ## use a for instead of repeating the sam code over and over, and over for btn_num in range(1, 11): if btn_num==10: btn_num=0 btn_instance = Button(Form,text=btn_num, width=20, height=7, command=partial(self.setit, btn_num)) ## send btn num to function btn_instance.grid(row=this_row, column=this_column) self.btn_dict[btn_num]=btn_instance this_column += 1 if this_column > 2: this_column=0 this_row += 1 self.btn_cancel = Button(Form,text="Cancel/Clear",bg="red",width=20,height=7,command =self.clear) self.btn_cancel.grid(row=4,column=0) ## self.btn_login = Button(Form,text="Login",bg="green",width=20,height=7,command = lambda:login()) ## self.btn_login.grid(row=4,column=2) #Functions def setit(self, num): print(num) self.btn_dict[num].config(text="") def set_text(self, text): widget = e.focus_get() if widget in e.entries: widget.insert(0, text) def clear(self): self.tb_pin.delete(0,END) root = Tk() root.title("FedUni Banking") root.geometry("440x640") app = App(root) root.mainloop() RE: AttributeError: 'App' object has no attribute 'set_text' - Barrowman - Jun-15-2018 For each button you are doing this: self.btn_1 = Button(Form,text="1",width=20,height=7So that sets the text on the button to ( in this case ) 1 Then you are expecting the rest of that line ,command = lambda:self.set_text("1"))to set the text to ( in this case ) 1 which is what it is already set to. What were you expecting to happen? RE: AttributeError: 'App' object has no attribute 'set_text' - Sahil1313 - Jun-16-2018 My Application is working.The GUI works correctly but when I press the numpad buttons it show the following error: Exception in Tkinter callback Traceback (most recent call last): File "C:\Python3\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) File "mainclass.py", line 32, in <lambda> self.btn_2 = Button(Form,text="2",width=20,height=7,command = lambda:self.set_text("2")) AttributeError: 'App' object has no attribute 'set_text' RE: AttributeError: 'App' object has no attribute 'set_text' - Larz60+ - Jun-16-2018 This error no longer matches the code provided in post 1. Please post the updated code that matches error messages. when posting errors, please include the full, verbatim, message as it includes valuable information on program flow prior to the error occurrence, and enclose in error tags (highlight code, and click 'circle with X' icon). RE: AttributeError: 'App' object has no attribute 'set_text' - woooee - Jun-17-2018 Quote:self.btn_2 = Button(Form,text="2",width=20,height=7,command = lambda:self.set_text("2"))Already answered .If you are not going to read responses then there is no reason to post further. |