Search code examples
python-3.xtkinterself

AttributeError: 'Demo1' object has no attribute 'textbox'


I am calling a function (fun1) from the first class(Demo1) and from that called function(fun1) calling the another function (data) of another class(Demo2) to send the result but i am getting the following error while trying to get that result in the textbox. I doubt that its because of the class self conflict. Can you please help me with the idea to solve this.

Please find the code:

from tkinter import ttk
import tkinter as tk
from tkinter import *


def fun1(self,name):
    result="check"
    Demo2.data(result)
    
def cal(master):
     master = Demo2(master)

class Demo1:
    
    def __init__(self, master):
        self.master = master

        button=tk.Button(self.master, text="check",anchor="w",command=lambda :fun1(self,"abc") )
        button.grid(row=0,column=1)
        button.config(command=lambda button=button: [cal(self.master),fun1(self,"abc")])
            
        
class Demo2:
    def __init__(self, master):
        self.master = master

        self.textbox=tk.Text(self.master,font=('Calibri',12))
        self.textbox.grid(row=0,column=1)
        

    def data(self,data):
        self.textbox.insert('end',data)


                            
def main():
    root = tk.Tk()
    app = Demo1(root)
    root.mainloop()


if __name__ == '__main__':
    main()        
 

 

Error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:/Users/Desktop/New folder/demo/che.py", line 16, in <lambda>
    self.button=tk.Button(self.master, text="check",anchor="w",command=lambda :fun1(self,"abc") )
  File "C:/Users/Desktop/New folder/demo/che.py", line 8, in fun1
    Demo2.data(self,result)
  File "C:/Users/Desktop/New folder/demo/che.py", line 29, in data
    self.textbox.insert('end',data) #Error
AttributeError: 'Demo1' object has no attribute 'textbox'

Solution

  • Here You go fixed Your code a bit:

    from tkinter import ttk
    import tkinter as tk
    from tkinter import *
    
    
    def fun1(parent, name):
        result = "check"
        master = Demo2(parent)
        master.data(result)
    
    
    class Demo1:
        def __init__(self, master):
            self.master = master
    
            button = tk.Button(self.master, text="check", anchor="w", command=lambda: fun1(self.master, "abc"))
            button.grid(row=0, column=1)
    
    
    class Demo2:
        def __init__(self, master):
            self.master = master
    
            self.textbox = tk.Text(self.master, font=('Calibri', 12))
            self.textbox.grid(row=0, column=1)
    
        def data(self, data):
            self.textbox.insert('end', data)
    
    
    def main():
        root = tk.Tk()
        Demo1(root)
        root.mainloop()
    
    
    if __name__ == '__main__':
        main()
    

    also You are not using name in fun1()