Ruben Ferreira
Ruben Ferreira

Reputation: 152

AttributeError: 'StandardFrame' object has no attribute 'tk'

I am building a form in which a user can capture the details of their business/company which is envisaged to be used later in the programme. I got the form working great - but I coded each Label Frame and every widget within those individually and I thought it would be best (as most of the widgets are standard to each other) to rather set them up as classes.

So in the main part of the programme I set up the window, the first frame and the first entry widget and its associated label as follows:

# Set up the Main Window
set_up_window = tk.Tk()

# Basic Business Details Label Frame
basic_details_frame = StandardFrame(set_up_window, "Basic Business Details")

# Trading Name
trading_name = StandardEntry1LineWide(basic_details_frame, "Trading Name:", 0, client_data_list[0])

The associated classes are as follows:

class StandardFrame:
def __init__(self, master, title):
    self.frame = tk.LabelFrame(master, text=title)
    self.frame.grid(column=0, row=2, padx=10, pady=10, ipadx=10, sticky="nesw")
    self.frame.grid_columnconfigure(0, weight=1, minsize=250)
    self.frame.grid_columnconfigure(1, weight=3)
    self.frame.grid_columnconfigure(2, weight=3)


class StandardEntry1LineWide:
def __init__(self, master, label_text, row, pre_pop_data):
    self.label = tk.Label(master, text=label_text, font=('​Helvetica', 11, 'bold'))
    self.label.grid(column=0, row=row, sticky="w")
    self.entry = tk.Text(master, height=1, font=('​Helvetica', 10, 'normal'))
    self.entry.insert(tk.END, pre_pop_data)
    self.entry.grid(column=1, row=row, columnspan=2, sticky="nesw", padx=10)
    self.entry.bind("<Tab>", self.MovetoNextField)

def MovetoNextField(self, event):
    event.widget.tk_focusNext().focus()
    return "break"

When I debug the programme the Label Frame seems to be set up no issues, but when I try to build the first entry widget I get the following error:

Traceback (most recent call last):
  File "C:\Users\27826\PycharmProjects\pythonProject\ERP System\StartWindowModule.py", line 29, in 
    CheckClientData CompanyInfoFormV2.FormBuilder(ClientDataList, FirstTimeEntry)
  File "C:\Users\27826\PycharmProjects\pythonProject\ERP System\CompanyInfoFormV2.py", line 132, in 
    FormBuilder trading_name = StandardEntry1LineWide(basic_details_frame, "Trading Name:", 0, 
    client_data_list[0])
  File "C:\Users\27826\PycharmProjects\pythonProject\ERP System\CompanyInfoFormV2.py", line 25, in 
    __init__ self.label = tk.Label(master, text=label_text, font=('​Helvetica', 11, 'bold'))
  File "C:\Users\27826\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 3144, in 
    __init__ Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\27826\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2562, in 
    __init__ BaseWidget._setup(self, master, cnf)
  File "C:\Users\27826\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2531,  
    _setup self.tk = master.tk
  AttributeError: 'StandardFrame' object has no attribute 'tk'

I have been tinkering for about an hour and can't seem to fix it - can anyone assist please?

Upvotes: 0

Views: 112

Answers (1)

acw1668
acw1668

Reputation: 46669

StandardFrame is not a tkinter container (i.e. Frame, Toplevel, etc), so it can be used as the parent of other widgets. So the following line:

trading_name = StandardEntry1LineWide(basic_details_frame, "Trading Name:", 0, client_data_list[0])

should be changed to:

trading_name = StandardEntry1LineWide(basic_details_frame.frame, "Trading Name:", 0, client_data_list[0])

Better solution is to make StandardFrame inherits from LabelFrame directly:

class StandardFrame(tk.LabelFrame):
    def __init__(self, master, title):
        super().__init__(master, text=title)
        self.grid(column=0, row=2, padx=10, pady=10, ipadx=10, sticky="nesw")
        self.grid_columnconfigure(0, weight=1, minsize=250)
        self.grid_columnconfigure(1, weight=3)
        self.grid_columnconfigure(2, weight=3)

Upvotes: 1

Related Questions