PinPointPng
PinPointPng

Reputation: 11

Tkinter OOP Buttons causing an AttributeError I don't understand

I have a bit of unfinished code because I got stuck on a Traceback Error:

Traceback (most recent call last):
  File "D:/Documents/Python/Code Check/Code Check.py", line 40, in <module>
    root = codeCheck()
  File "D:/Documents/Python/Code Check/Code Check.py", line 10, in __init__
    main = mainFrame(self, padding=(60, 30))
  File "D:/Documents/Python/Code Check/Code Check.py", line 30, in __init__
    checkCodeButton = ttk.Button(self, text="Check Code", command=self.checkCode)
AttributeError: 'mainFrame' object has no attribute 'checkCode'

Here is my code:

import tkinter as tk
from tkinter import ttk

class codeCheck(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.title("Code Check")

        main = mainFrame(self, padding=(60, 30))
        main.grid()


class mainFrame(ttk.Frame):
    def __init__(self, container, **kwargs):
        super().__init__(container, **kwargs)

        def checkCode(self, *args):
            print("hi")

        self.inputedCode = tk.StringVar()
        self.code = "123"

        enterCodeLabel = ttk.Label(self, text="Code: ")
        codeEntry = ttk.Entry(self, textvariable=self.inputedCode)

        enterCodeLabel.grid(row=0, column=0)
        codeEntry.grid(row=0, column=1)

        showCodeButton = ttk.Button(self, text="Show Code")
        checkCodeButton = ttk.Button(self, text="Check Code", command=self.checkCode)

        showCodeButton.grid(row=1, column=0)
        checkCodeButton.grid(row=1, column=1)



root = codeCheck()
root.mainloop()

I'm following a Udemy course and copied the script almost exactly. I am pretty new to python tkinter and i'm stuck. What did I do wrong?

Upvotes: 1

Views: 46

Answers (1)

Ayoub Benayache
Ayoub Benayache

Reputation: 1164

Remove checkCode() function from your mainframe constructor.

import tkinter as tk
from tkinter import ttk

class codeCheck(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.title("Code Check")

        main = mainFrame(self, padding=(60, 30))
        main.grid()


class mainFrame(ttk.Frame):
    def __init__(self, container, **kwargs):
        super().__init__(container, **kwargs)



        self.inputedCode = tk.StringVar()
        self.code = "123"

        enterCodeLabel = ttk.Label(self, text="Code: ")
        codeEntry = ttk.Entry(self, textvariable=self.inputedCode)

        enterCodeLabel.grid(row=0, column=0)
        codeEntry.grid(row=0, column=1)

        showCodeButton = ttk.Button(self, text="Show Code")
        checkCodeButton = ttk.Button(self, text="Check Code", command=self.checkCode) 

        showCodeButton.grid(row=1, column=0)
        checkCodeButton.grid(row=1, column=1)

    def checkCode(self, *args): # place this function here out of init
        print("hi")

root = codeCheck()
root.mainloop()

Upvotes: 1

Related Questions