tkinter how do i understand errors that dont say whats wrong?

Viewed 53

when I try to run my code, I get a lot of errors, and i don't even know what these are saying. there's a few errors and all just say the code and doesn't say the error: I'll put down the error message and also my code, please tell me what the errors are and how I can fix it, thanks. I just started this code which is why there are so little things and I'm sort of new to Tkinter.

Error Message.

  File "C:/MathsQuiz/venv/mainmaths.py", line 31, in <module>
    instance = Starting(root)
  File "C:/MathsQuiz/venv/mainmaths.py", line 13, in __init__
    self.usern = Label(root.frame, text="Please enter a username", font=("16"))
  File "C:\Users\marky\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3143, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\marky\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2561, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Users\marky\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2530, in _setup
    self.tk = master.tk
AttributeError: 'function' object has no attribute 'tk'

Process finished with exit code 1

Here is my code.

from tkinter import *
import random

x = random.randint(5, 12)
y = random.randint(5, 12)
class Starting:
    def __init__(self, master):
        self.frame = Frame(master, padx=200, pady=200)
        self.frame.grid()
        self.title = Label(self.frame, text="Multi-level Maths Quiz", font=("Helvetica", "20", "bold"))
        self.title.grid(row=0, padx=30, pady=30)

        self.usern = Label(root.frame, text="Please enter a username", font=("16"))
        self.usern.grid(row=1, padx=20, pady=20)
        self.userentry = Entry(self.frame, width=50)
        self.userentry.grid(row=2)
        self.usercont = Button(self.frame, text="Continue", COMMAND=clear1)
        self.usercont.grid(row=3)

    def clear1(self):
        self.frame.destroy()

if __name__ == "__main__":
   root = Tk() 
   root.title = ("Maths Quiz")
   instance = Starting(root)
   root.mainloop()
1 Answers

There are a few issues with your code.

  1. I quite don't get why you are using root.frame as a parent for the username label It should be self.frame.

  2. In line Button(self.frame, text="Continue", COMMAND=clear1), clear1 is not define according to python, you need to use self.clear1` to refer to the class methods.

  3. I'm guessing, you're probably using some IDLE as using COMMAND as an argument name doesn't work like that. It should be like Button(self.frame, text="Continue", command=self.clear1) or it can be like Button(self.frame, {COMMAND: self.clear1}, text="Continue").


Complete code:

from tkinter import *
import random

x = random.randint(5, 12)
y = random.randint(5, 12)

class Starting:
    def __init__(self, master):
        self.frame = Frame(master, padx=200, pady=200)
        self.frame.grid()
        self.title = Label(self.frame, text="Multi-level Maths Quiz", 
                           font=("Helvetica", "20", "bold"))
        self.title.grid(row=0, padx=30, pady=30)

        self.usern = Label(self.frame,text="Please enter a username", font=("16"))
        self.usern.grid(row=1, padx=20, pady=20)
        self.userentry = Entry(self.frame, width=50)
        self.userentry.grid(row=2)
        self.usercont = Button(self.frame, text="Continue", command=self.clear1)
        self.usercont.grid(row=3)

    def clear1(self):
        self.frame.destroy()

if __name__ == "__main__":
   root = Tk() 
   root.title = ("Maths Quiz")
   instance = Starting(root)
   root.mainloop()
Related