Why is Label object not callable appearing

Viewed 25

I am in the process of making an OOP Countdown GUI with the letters, numbers and conundrum games. This is a snippet of my code so far.

  class App(tk.Tk):
    def __init__(self):
      super().__init__()
      self.title("Main")
      self.configure(bg=LIGHT_BLUE)
      self.title=tk.Label(master=self,bg=LIGHT_BLUE,font=("Arial",12,"bold"),text="!Countdown Games!",pady=5,padx=5)
      self.title.pack()
      self.frame=tk.Frame(master=self,bg=LIGHT_BLUE)
      self.LettersButton=tk.Button(master=self.frame,bg=LIGHT_BLUE,font=("Arial",12,"bold"),text="Letters",command=self.start_letters)
      self.LettersButton.grid(row=0,column=0,sticky="NESW",pady=10,padx=2)
      self.NumbersButton=tk.Button(master=self.frame,bg=LIGHT_BLUE,font=("Arial",12,"bold"),text="Numbers",command=self.start_numbers)
      self.NumbersButton.grid(row=0,column=1,sticky="NESW",pady=10,padx=2)
      self.LettersButton=tk.Button(master=self.frame,bg=LIGHT_BLUE,font=("Arial",12,"bold"),text="Conundrum",command=self.start_conundrum)
      self.LettersButton.grid(row=0,column=2,sticky="NESW",pady=10,padx=2)
      self.frame.pack()
      self.mainloop()

    def start_letters(self):
      self.withdraw()
      self.Letters=Letters()

    def start_numbers(self):
      self.withdraw()
      self.Numbers=Numbers()

    def start_conundrum(self):
      self.withdraw()
      self.Conundrum=Conundrum()


  class Conundrum(tk.Toplevel):
    def __init__(self):
      super().__init__()
      self.configure(bg=LIGHT_BLUE)
      self.geometry("400x150")
      self.title("Conundrum")
      self.frameA=tk.Frame(master=self,bg=LIGHT_BLUE)
      self.letterList=[]
      self.answer=r.choice(ninewords)
      self.anagram=anagram(self.answer)
      for x in range(9):
        self.letterList.append(ConundrumDisplay(self.frameA,x))
      self.frameA.pack()
      self.frameB=tk.Frame(master=self,bg=LIGHT_BLUE)
      self.letterEntryList=[]
      for y in range(9):
        self.letterEntryList.append(ConundrumEntry(self.frameB,y))
      self.frameB.pack()
      self.timer=tk.Label(master=self,bg=LIGHT_BLUE,font=("Arial",12,"bold"),text="31")
      self.update_timer()
      self.timer.pack()
      self.mainloop()
      m.showinfo("Startgame","The game will start when you press OK")
      for x in range(9):
        self.letterlist[x].add_letter(x,self.anagram)
      self.bind("<Key>",self.process_key)
      self.bind("Return",self.process_guess)
      self.bind("BackSpace",self.process_back)

The upper bit of code is a method from an App class which inherits from tk.Tk The error is in the super()._ _ init _ _() line in the conundrum class after you press the Conundrum button in the main wn This conundrum class is obviously the bit of code just below which inherits from tk.Toplevel. I have used tk.Toplevel before but it hasn't shown the error before. I have tried to reduce the amount of code that I am posting but if any more of it is necessary to figure out the error, then I can amend the question.

Error Message:

    Traceback (most recent call last):
      File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
        return self.func(*args)
      File "main.py", line 50, in start_conundrum
        self.Conundrum=Conundrum()
      File "main.py", line 55, in __init__
        super().__init__()
      File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/tkinter/__init__.py", line 2624, in __init__
        self.title(root.title())
    TypeError: 'Label' object is not callable
2 Answers

The error is telling you exactly what is wrong: you can't call a Label as if it was a function.

You are doing this: self.title(root.title()). root.title is an instance of Label but you are trying to call it as if it was a function. You get the same error if you do something like this:

foo = Label(...)
foo()

Since foo is a Label rather than a function, you can't call it.

If you want to call the title function of the root window, you should not name the label self.title. Name it something else so that you can use the title method of the base class.

class Conundrum extends tkinter.Toplevel, but Toplevel does not support geometry nor title.

The title and geometry goes into the tkinter.Tk part of your gui, often called the root element.

But thats all I can tell you with the little amount of code you give me.

From the documentation:

The toplevel command creates a new toplevel widget (given by the pathName argument). Additional options, described above, may be specified on the command line or in the option database to configure aspects of the toplevel such as its background color and relief. The toplevel command returns the path name of the new window. A toplevel is similar to a frame except that it is created as a top-level window: its X parent is the root window of a screen rather than the logical parent from its Tk path name. The primary purpose of a toplevel is to serve as a container for dialog boxes and other collections of widgets. The only visible features of a toplevel are its background color and an optional 3-D border to make the toplevel appear raised or sunken.

Related