1

my code was working fine before i added a block of code def startcheck(self):and i'm not sure what's wrong with it. please tell me what im doing wrong, thanks. here's what it says:

Traceback (most recent call last):
  File "C:/MathsQuiz/venv/test 2.py", line 138, in <module>
    instance = Starting(root)
  File "C:/MathsQuiz/venv/test 2.py", line 21, in __init__
    self.usercont = Button(self.frame, text="Continue", command=self.startcheck())
_tkinter.TclError: bad window path name ".!frame"

here's a small part of my code:

class Starting:
    def __init__(self, master):
    self.master = master
                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.name = StringVar()
        self.name.set(self.userentry.get())
        self.usercont = Button(self.frame, text="Continue", command=self.startcheck())
        self.usercont.grid(row=3)
    
    def startcheck(self):
        if self.name.get() == None:
            nameerror = Label(self.frame, text="Please enter a username")
            nameerror.grid(row=5)
        else:
            self.clear1()

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

if __name__ == "__main__":
   root = Tk()
   root.title = ("Maths Quiz")
   instance = Starting(root)
   root.mainloop()
Mark Lin
  • 103
  • 8

3 Answers3

3

A few things will get you up and running:

  • add self.frame = master as the first line to your __init__ method
  • change command=self.startcheck() to command=self.startcheck, the command argument expects a callable (whereas with the parentheses it's already been called)
  • The snippet doesn't have a Question1 class but I imagine that is somewhere else in your code
  • should use self.name.get() == "" instead of == None which should never be true as StringVar.get() returns a string. The other more concise way to do this is to use: if self.name.get(): {true condition} else {false condition}. This takes advantage of python's truthiness where the empty string evaluates to False
Tresdon
  • 1,211
  • 8
  • 18
0

There are indeed multiple things you need to correct. First and most important is that you are referring multiple times to self.frame which does not exists as your class is not inherited from tk.Frame. Below I fixed your code using master instead of self.frame:

from tkinter import *

class Starting:
    def __init__(self, master):
        self.usern = Label(master ,text="Please enter a username:", font=("16"))
        self.usern.grid(row=1, padx=20, pady=20)
        self.userentry = Entry(master, width=50)
        self.userentry.grid(row=2)
        self.name = StringVar()
        self.name.set(self.userentry.get())
        self.usercont = Button(master, text="Continue", command=lambda: self.startcheck(master))
        self.usercont.grid(row=3)
    
    def startcheck(self, master):
        if self.name.get() == None:
            nameerror = Label(master, text="Please enter a username")
            nameerror.grid(row=5)
        else:
            self.clear1(master)

    def clear1(self, master):
        master.destroy()
        Question1(root)

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

Also command = self.startcheck() will not work because of the parentheses (it calls the function, rather than specify it as a call-back function). Above I passed master to functions, but instead you can also include self.master = master in __init__() and then refer to self.master instead. In that case, you don't have to pass master as parameter.

# Obviously it throws an error when arriving at Question1
Ronald
  • 2,930
  • 2
  • 7
  • 18
-2

There are multiple issues:

question.py:1:0: W0401: Wildcard import tkinter (wildcard-import)
question.py:5:27: E1101: Instance of 'Starting' has no 'frame' member (no-member)
question.py:8:31: E1101: Instance of 'Starting' has no 'frame' member (no-member)
question.py:12:31: E1101: Instance of 'Starting' has no 'frame' member (no-member)
question.py:4:23: W0613: Unused argument 'master' (unused-argument)
question.py:16:11: C0121: Comparison to None should be 'expr is None' (singleton-comparison)
question.py:17:30: E1101: Instance of 'Starting' has no 'frame' member (no-member)
question.py:24:8: E1101: Instance of 'Starting' has no 'frame' member (no-member)
question.py:25:8: E0602: Undefined variable 'Question1' (undefined-variable)

stovfl
  • 14,998
  • 7
  • 24
  • 51