-2

I have class which has ttk components. When i run this class from some reason it raise an error, AttributeError: 'Accounts' object has no attribute 'tk'. I cant understand why.

CODE

from tkinter import *
from tkinter import ttk, messagebox
import tkinter.font as tkfont
from database.db import Database

class Accounts:
def __init__(self, window):
    self.window = window
    self.username = StringVar()
    self.password = StringVar()
    self.name = StringVar()
    self.phone = StringVar()
    self.SAVE = 1
    self.UPDATE = 0

    self.headerFont = tkfont.Font(family="Helvetica", size=12, weight='bold')
    self.titleFont = tkfont.Font(family="Helvetica", size=9)
    self.h3 = tkfont.Font(family="Helvetica", size=11, weight='bold')
    self.h4 = tkfont.Font(family="Helvetica", size=10, weight='bold')
    self.bold = tkfont.Font(weight='bold', size=10)
    self.list_box_text = tkfont.Font(family="Verdana", size=11, weight='bold')

    # Add account form (left side)
    ttk.Label(self, text='Manage your accounts', font=self.headerFont).grid(column=0, row=0, padx=10, pady=10)
    ttk.Label(self, text='Add account', font=self.h3).grid(column=0, row=1, padx=10, pady=10, columnspan=2)

It is always crash on this line ttk.Label(self, text='Manage your accounts', font=self.headerFont).grid(column=0, row=0, padx=10, pady=10). Cant understand why its crashing, I import ttk. What am i doing wrong?

E.Bolandian
  • 553
  • 10
  • 35
  • 1
    I think the problem is because of how you're initializing the Label. `ttk.Label`'s first argument is `parent` where you give `self` as in, the instance of `Account`. The `parent` parameter has to be a `tk` object that the label can "bind" against. – Hampus Larsson Apr 16 '20 at 11:30

1 Answers1

4

This is because you are trying to make the ttk.Label a child of self, which is not a widget. Did you mean self.window?

    ttk.Label(self.window, text='Manage your accounts', font=self.headerFont).grid(column=0, row=0, padx=10, pady=10)
    ttk.Label(self.window, text='Add account', font=self.h3).grid(column=0, row=1, padx=10, pady=10, columnspan=2)
Maximouse
  • 4,170
  • 1
  • 14
  • 28