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'
#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


Messages In This Thread
RE: AttributeError: 'App' object has no attribute 'set_text' - by woooee - Jun-15-2018, 03:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  'NoneType' object has no attribute 'get' zunebuggy 8 1,544 Oct-13-2023, 06:39 PM
Last Post: zunebuggy
  tkinter AttributeError: 'GUI' object has no attribute pfdjhfuys 3 1,673 May-18-2023, 03:30 PM
Last Post: pfdjhfuys
  [Kivy] Windows 10: AttributeError: 'WM_PenProvider' object has no attribute 'hwnd' mikepy 1 2,412 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,670 Aug-30-2022, 08:44 AM
Last Post: tompranks
  AttributeError: 'NoneType' object has no attribute 'get' George87 5 15,732 Dec-23-2021, 04:47 AM
Last Post: George87
  [PyQt] AttributeError: 'NoneType' object has no attribute 'text' speedev 9 11,605 Sep-25-2021, 06:14 PM
Last Post: Axel_Erfurt
  [Tkinter] AttributeError: '' object has no attribute 'tk' Maryan 2 14,858 Oct-29-2020, 11:57 PM
Last Post: Maryan
  [Tkinter] AttributeError: 'tuple' object has no attribute 'replace' linuxhacker 7 6,966 Aug-08-2020, 12:47 AM
Last Post: linuxhacker
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 6,353 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' edphilpot 5 9,295 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