Python Forum
[Tkinter] AttributeError: 'App' object has no attribute 'set_text'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] AttributeError: 'App' object has no attribute 'set_text'
#1
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()
Reply
#2
use:
def set_text(self, e,text):
and
def clear(self)
Reply
#3
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()  
Reply
#4
For each button you are doing this:
self.btn_1 = Button(Form,text="1",width=20,height=7
So 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?
Reply
#5
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'
Reply
#6
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).
Reply
#7
Quote: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'
Already answered .If you are not going to read responses then there is no reason to post further.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  'NoneType' object has no attribute 'get' zunebuggy 8 1,244 Oct-13-2023, 06:39 PM
Last Post: zunebuggy
  tkinter AttributeError: 'GUI' object has no attribute pfdjhfuys 3 1,455 May-18-2023, 03:30 PM
Last Post: pfdjhfuys
  [Kivy] Windows 10: AttributeError: 'WM_PenProvider' object has no attribute 'hwnd' mikepy 1 2,247 Feb-20-2023, 09:26 PM
Last Post: deanhystad
  [Tkinter] Can't update label in new tk window, object has no attribute tompranks 3 3,468 Aug-30-2022, 08:44 AM
Last Post: tompranks
  AttributeError: 'NoneType' object has no attribute 'get' George87 5 15,149 Dec-23-2021, 04:47 AM
Last Post: George87
  [PyQt] AttributeError: 'NoneType' object has no attribute 'text' speedev 9 11,248 Sep-25-2021, 06:14 PM
Last Post: Axel_Erfurt
  [Tkinter] AttributeError: '' object has no attribute 'tk' Maryan 2 14,447 Oct-29-2020, 11:57 PM
Last Post: Maryan
  [Tkinter] AttributeError: 'tuple' object has no attribute 'replace' linuxhacker 7 6,759 Aug-08-2020, 12:47 AM
Last Post: linuxhacker
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 6,191 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' edphilpot 5 9,110 Dec-20-2019, 09:52 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020