Python Forum
[Tkinter] How to find what causing AttributeError: '_tkinter.tkapp' ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to find what causing AttributeError: '_tkinter.tkapp' ?
#1
I am getting an error AttributeError: '_tkinter.tkapp' object has no attribute 'file1' and i have no idea how to fix it. Could any one Point me in the right direction or help me out. I marked out in the code whats triggering the error.


The full error msg

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\xzenon\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__
    return self.func(*args)
  File "C:\Users\xzenon\Desktop\frame switching - kopia.py", line 60, in make
    self.file1.writelines('{}.{}\n'.format(value1, parts+1))
  File "C:\Users\xzenon\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1949, in __getattr__
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'file1'
And here is the code.


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

TITLE_FONT = ("Helvetica", 18, "bold")

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.öppna_data_filer()
        
        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()

    def öppna_data_filer(self):
        self.file1 = open('partmärkning.txt', 'wt')
        self.file2 = open('kabelmärking.txt', 'wt')
      
    #Vad ska göras när programet stängs av
    def On_exit(self):
        #stänger ner data filer och sparar data 
        self.file1.close()
        self.file2.close()
        #stäng av prgramet
        root.destroy()      

    #funktioner This is what triggering the error.
    def make(self, *args):
        try:
            a = self.kabelsort.get()
            value1 = (self.kabel.get())
            value2 = (self.part.get())
            value3 = (self.ränsa.get())
            if (a == 1):
                   for parts in range(value2):
                       self.file1.writelines('{}.{}\n'.format(value1, parts+1))
                   self.file1.writelines('{}.{}\n'.format(value1, "SC"))    
                   self.file2.writelines('{}\n'.format(value1))
                   self.listbox.insert(END, value1)
                   print(value1)

            elif (a == 2):
                   for parts in range(value2):
                       self.file1.writelines('{}.{}\n'.format(value1, parts+1))
                   self.file2.writelines('{}\n'.format(value1))
                   self.listbox.insert(END, value1)
                   print(value1)

            elif (a == 3):
                   if (value2 == 3):
                       self.file1.writelines('{}.{}\n'.format(value1, "BN"))
                       self.file1.writelines('{}.{}\n'.format(value1, "BU"))
                       self.file1.writelines('{}.{}\n'.format(value1, "YE/GN"))
                       self.file1.writelines('{}.{}\n'.format(value1, "SC"))
                       self.file2.writelines('{}\n'.format(value1))
                       self.listbox.insert(END, value1)
                       print(value1)                   
                   elif (value2 == 5):
                       self.file1.writelines('{}.{}\n'.format(value1, "BN"))
                       self.file1.writelines('{}.{}\n'.format(value1, "BK"))
                       self.file1.writelines('{}.{}\n'.format(value1, "GY"))
                       self.file1.writelines('{}.{}\n'.format(value1, "BU"))
                       self.file1.writelines('{}.{}\n'.format(value1, "YE/GN"))
                       self.file1.writelines('{}.{}\n'.format(value1, "SC"))    
                       self.file2.writelines('{}\n'.format(value1))
                       self.listbox.insert(END, value1)
                       print(value1)
            if (value3 == 1):
                   self.kabelnamn_entry.delete(0, END)
                   self.part_entry.delete(0, END)                 
                        
        except ValueError:
            pass

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame("PageOne"))
        button1.pack()



class PageOne(tk.Frame, SampleApp):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        #fönster designern här bästems fönster ramar, rader och columner
        mainframe = ttk.Frame(self, padding="4 4 12 12")
        mainframe.grid(column=1, row=0, sticky=(N, W, E, S))
        mainframe.columnconfigure(0, weight=1)
        mainframe.rowconfigure(0, weight=1)

        frame = ttk.Frame(self, relief=SUNKEN, padding="2 2 0 12")
        frame.grid(column=0, row=0, sticky=(N, W, E, S))
        frame.grid_rowconfigure(0, weight=1)
        frame.grid_columnconfigure(0, weight=1)


        #data strings som används av funktioner
        self.kabel = StringVar()
        self.part = IntVar()
        self.kabelsort = IntVar()
        self.kabelsort.set(1)
        self.ränsa = IntVar()

        #fönster designern. här bestäms vad du ska ha och vart det ska placeras etc
        #--------------------------------------------------------------------------
        #etikettera, textrutor
        ttk.Label(mainframe, text="Kabelnamn:").grid(column=1, row=0, sticky=(W, E), columnspan=2)
        self.kabelnamn_entry = tk.Entry(mainframe, textvariable=self.kabel)
        self.kabelnamn_entry.grid(column=1, row=1, sticky=(W, E), columnspan=3)

        ttk.Label(mainframe, text="Parter:").grid(column=1, row=2, sticky=(W, E), columnspan=2)
        self.part_entry = tk.Entry(mainframe, textvariable=self.part)
        self.part_entry.grid(column=1, row=3, sticky=(W, E), columnspan=3)
        self.part_entry.delete(0, END)

        #radioknappar, checkbuttons
        sort1 = tk.Radiobutton(mainframe, text='Skärmad kabel', variable=self.kabelsort, value='1').grid(column=2, row=4, sticky=W)
        sort2 = tk.Radiobutton(mainframe, text='Oskärmad', variable=self.kabelsort, value='2').grid(column=3, row=4, sticky=W)
        sort3 = tk.Radiobutton(mainframe, text='Eklk/Eqlq färgade', variable=self.kabelsort, value='3').grid(column=2, row=5, sticky=W)
        sort4 = tk.Checkbutton(mainframe, text='Ränsa fält', variable=self.ränsa, onvalue=1, offvalue=0).grid(column=3, row=5, sticky=W)

        #Listbox
        self.listbox = Listbox(frame, height=10)
        self.listbox.grid(column=0, row=0, rowspan=6, sticky=(N, W, S))

        # create a vertical scrollbar to the right of the listbox
        scrollbar = tk.Scrollbar(frame, orient=VERTICAL)
        scrollbar.grid(column=1, row=0, rowspan=6,  sticky=(N, E, S))

        # koppla ihop listbox med scrollbar
        self.listbox.config(yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.listbox.yview)


        #knappar
        b1 = tk.Button(mainframe, text="Make", command=self.make).grid(column=3, row=6,sticky=E)
        b2 = tk.Button(mainframe, text="About", command=self.about).grid(column=2, row=6,sticky=W)


        for child in mainframe.winfo_children():
            child.grid_configure(padx=5, pady=5)
       

    def about(self, *args):
        top1 = Toplevel()
        top1.title("About")
        top1.resizable(width=False, height=False)
        #fönster designern här bästems fönster ramar, rader och columner
        frame2 = tk.Frame(top1, padding="3 3 12 12")
        frame2.grid(column=1, row=0, sticky=(N, W, E, S))
        frame2.columnconfigure(0, weight=1)
        frame2.rowconfigure(0, weight=1)

        tk.Label(frame2, text="KabelMärkarn", font=('', 15)).grid(column=0, row=0, columnspan=1)
        tk.Label(frame2, text="Version: 2.0").grid(column=0, row=1, sticky=W)
        tk.Label(frame2, text="Skapad och ägs av Mikael Gharib.").grid(column=0, row=2, sticky=W, columnspan=2)




if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
Reply
#2
Yes, a bit strange? I would add a print instruction in öppna_data_filer to check that it is actually executed:


    def öppna_data_filer(self):
         self.file1 = open('partmärkning.txt', 'wt')
         self.file2 = open('kabelmärking.txt', 'wt')
         print(self.file1) 
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
I think you have a multiple inheritance problem due to

class PageOne(tk.Frame, SampleApp):
inheriting from both tk.Frame and SampleApp
The __init__ of SampleApp is not getting called which in turn means the method oppna_data_filer is not getting called, hence self.file1 attribute is not defined.
Reply
#4
(Sep-18-2016, 09:49 AM)Ofnuts Wrote: Yes, a bit strange? I would add a print instruction in öppna_data_filer to check that it is actually executed:


    def öppna_data_filer(self):
         self.file1 = open('partmärkning.txt', 'wt')
         self.file2 = open('kabelmärking.txt', 'wt')
         print(self.file1) 

I know that its executed by watching the file getting made.
Reply
#5
The execution of the files getting made is because the call to

app = SampleApp()
Reply
#6
(Sep-18-2016, 10:17 AM)Yoriz Wrote: The execution of the files getting made is because the call to

app = SampleApp()



But shouldent then for examle self.file1.writelines('{}.{}\n'.format(value1, parts+1)) work?
Reply
#7
No because of the inheritance problem I stated above, the instance of SampleApp has a file1 attribute but the instance of PageOne does not because it has only called the tk.Frame's __init__ and not the SampleApp's __init__.
Reply
#8
(Sep-18-2016, 10:37 AM)Yoriz Wrote: No because of the inheritance problem I stated above, the instance of SampleApp has a file1 attribute but the instance of PageOne does not because it has only called the tk.Frame's __init__ and not the SampleApp's __init__.

Ok may i ask how i should do to solve it? because i got minimal knowledge about inheritance etc in python.
Reply
#9
I see this is a continuation from this post
I suggest that you don't inherit SampleApp but instead pass an instance of SampleApp to PageOne.

This is an example of what you are trying to do
class Frame:
 
    def __init__(self):
        print('Frame')
 
 
class SampleApp:
 
    def __init__(self):
        print('SampleApp')
        self.oppna_data_filer()
        PageOne()
         
    def oppna_data_filer(self):
        self.file1 = 'file1'
 
 
class PageOne(Frame, SampleApp):
 
    def __init__(self):
        Frame.__init__(self)
        print('PageOne')
        print(self.file1)
 
one = SampleApp()
which will give
SampleApp
Frame
PageOne
AttributeError: 'PageOne' object has no attribute 'file1'
This is an example of how it would look passing PageOne the instance of SimpleApp

class Frame:
  
    def __init__(self):
        print('Frame')
  
  
class SampleApp:
  
    def __init__(self):
        print('SampleApp')
        self.oppna_data_filer()
        PageOne(self)
          
    def oppna_data_filer(self):
        self.file1 = 'file1'
  
  
class PageOne(Frame):
  
    def __init__(self, sample_app):
        Frame.__init__(self)
        print('PageOne')
        self.sample_app = sample_app
        print(self.sample_app.file1)
  
one = SampleApp()
which will give
SampleApp
Frame
PageOne
file1
P.S. Class Intermediate: Inheritance
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] _tkinter.TclError: can't invoke "destroy" command: application has been destroyed knoxvilles_joker 6 15,274 Apr-25-2021, 08:41 PM
Last Post: knoxvilles_joker
  [Tkinter] _tkinter.TclError: bitmap "Icon.gif" not defined djwilson0495 2 12,859 Aug-05-2020, 02:27 PM
Last Post: wuf
  [Tkinter] _tkinter.TclError: image "pyimage2" doesn't exist Killdoz 1 10,515 May-30-2020, 09:48 AM
Last Post: menator01
  AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' edphilpot 5 9,105 Dec-20-2019, 09:52 PM
Last Post: joe_momma
  "ModuleNotFoundError: No module named '_tkinter' in Python 3.8 Alfa 0 programmerc 1 6,283 Oct-21-2018, 06:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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