Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter error object has no attribute

So I am making a program similar to the arcade games. I want the lableGuess to appear in the toplevel window after clicking the frame but it gives me this error:

AttributeError: 'Window' object has no attribute 'window'

Here's the code:

from tkinter import *
from tkinter import font
import time

class Window(Frame):

    def __init__(self, master):

        Frame.__init__(self, master)
        self.master = master

        master.title("Arcade Games")
        master.geometry("800x600+560+240")

        b = Button(self, text="Guess the number", command=self.new_window)
        b.pack(side="top")
        self.customFont = font.Font(master, font="Heraldica", size=12)

        self.guess_number()

    def new_window(self):

        id = "Welcome to the 'Guess your number' game!\nAll you need to do is follow the steps\nand I will guess your number!\n\nClick anywhere to start!"
        self.window = Toplevel(self.master)
        frame = Frame(self.window)
        frame.bind("<Button-1>", self.guess_number)
        frame.pack()
        self.window.title("Guess the number")
        self.window.geometry("400x300+710+390")
        label = Label(self.window, text=id, font=self.customFont)
        label.pack(side="top", fill="both", padx=20, pady=20)

    def guess_number(self):


        labelGuess = Label(self.window, text="Pick a number between 1 and 10", font=self.customFont)
        time.sleep(2)
        labelGuess.pack(fill=BOTH, padx=20, pady=20)

if __name__ == "__main__":
    root = Tk()
    view = Window(root)
    view.pack(side="top", fill="both", expand=True)
    root.mainloop()
like image 560
ToucaN Avatar asked Sep 20 '26 17:09

ToucaN


1 Answers

Your initial call to guess_number in your initializer method is probably being invoked before you press the button and trigger the new_window event callback. In guess_number you're trying to pass self.window as an argument to Label() but it would be undefined at that time.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!