Python Forum
[Tkinter] AttributeError: '' object has no attribute 'tk'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] AttributeError: '' object has no attribute 'tk'
#1
I had to seperate this file from the main app, because had some errors that I had to fix them first. However, when I run the app I'm getting AttributeError. What am I missing?

from tkinter import *
from tkinter import Tk
from tkinter.ttk import Combobox
from tkcalendar import *
from tkinter import messagebox, filedialog
import sqlite3

class AddEmp():
    def __init__(self):
        ### FRAMES ###

        # Top Frame - Header
        self.topFrame = Frame(self, height = 100, bg = '#333')
        self.topFrame.pack(fill = X)

        # Center Frame - Buttons
        self.centerFrame= Frame(self, height = 750, bg = '#121212')
        self.centerFrame.pack(fill = X)

        # Top Frame - Data #
        # Header in Top Frame
        self.header_image = PhotoImage(file = 'img/add_employee.png')
        self.header_lbl = Label(self.topFrame, image = self.header_image, bg = '#333')
        self.header_lbl.grid(row = 0, column = 0, padx = 15, pady = 15)
        self.header_introduction = Label(self.topFrame, text = 'Add Employee', fg = '#b3b3b3', bg = '#333')
        self.header_introduction.grid(row = 0, column = 1)

        ######

root = Tk()
app = AddEmp()
root.title('All Fields are required')
root.geometry('440x823+700+150')
root.resizable(False, False)
root.mainloop()
Reply
#2
Are you sure this ever worked? AppEmp is not any kind of window, but you pass it as the parent to a frame (as self).

I am not sure how you go about fixing this. Is AddEmp a frame? Is it a dialog? I am going to make it a frame.
from tkinter import *
from tkinter import Tk
 
class AddEmp(Frame):  # Need some kind of frame or window as superclass
    def __init__(self, *args, **kvargs):
        super().__init__(*args, **kvargs)  # Initialize superclass
        self.pack(fill='both', expand=True)  # Pack self in parent

        # Now you can add widgets to self because self is a frame.
        self.header_lbl = Label(self, text='Label 1')
        self.header_lbl.grid(row = 0, column = 0, padx = 15, pady = 15)
        self.header_introduction = Label(self, text = 'Label 2')
        self.header_introduction.grid(row = 1, column = 0)
 
root = Tk()
AddEmp(root)
root.title('All Fields are required')
root.geometry('100x100')
root.mainloop()
Reply
#3
It's dialog from the main app. On click this one popup. Altought I know the basics to create window, I have still trouble when there is class involved, like in this case. I'm not sure where the geometry should be inside the class, before the class or bottom like I did in the post.

(Oct-29-2020, 11:40 PM)deanhystad Wrote: Are you sure this ever worked? AppEmp is not any kind of window, but you pass it as the parent to a frame (as self).

I am not sure how you go about fixing this. Is AddEmp a frame? Is it a dialog? I am going to make it a frame.
from tkinter import *
from tkinter import Tk
 
class AddEmp(Frame):  # Need some kind of frame or window as superclass
    def __init__(self, *args, **kvargs):
        super().__init__(*args, **kvargs)  # Initialize superclass
        self.pack(fill='both', expand=True)  # Pack self in parent

        # Now you can add widgets to self because self is a frame.
        self.header_lbl = Label(self, text='Label 1')
        self.header_lbl.grid(row = 0, column = 0, padx = 15, pady = 15)
        self.header_introduction = Label(self, text = 'Label 2')
        self.header_introduction.grid(row = 1, column = 0)
 
root = Tk()
AddEmp(root)
root.title('All Fields are required')
root.geometry('100x100')
root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  'NoneType' object has no attribute 'get' zunebuggy 8 1,358 Oct-13-2023, 06:39 PM
Last Post: zunebuggy
  tkinter AttributeError: 'GUI' object has no attribute pfdjhfuys 3 1,536 May-18-2023, 03:30 PM
Last Post: pfdjhfuys
  [Kivy] Windows 10: AttributeError: 'WM_PenProvider' object has no attribute 'hwnd' mikepy 1 2,308 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,530 Aug-30-2022, 08:44 AM
Last Post: tompranks
  AttributeError: 'NoneType' object has no attribute 'get' George87 5 15,404 Dec-23-2021, 04:47 AM
Last Post: George87
  [PyQt] AttributeError: 'NoneType' object has no attribute 'text' speedev 9 11,376 Sep-25-2021, 06:14 PM
Last Post: Axel_Erfurt
  [Tkinter] AttributeError: 'tuple' object has no attribute 'replace' linuxhacker 7 6,852 Aug-08-2020, 12:47 AM
Last Post: linuxhacker
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 6,257 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' edphilpot 5 9,171 Dec-20-2019, 09:52 PM
Last Post: joe_momma
  [Tkinter] AttributeError: 'App' object has no attribute 'set_text' Sahil1313 6 12,055 Jun-17-2018, 05:01 AM
Last Post: woooee

Forum Jump:

User Panel Messages

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