Nameerror: name 'Window' is not defined

Asked

Viewed 463 times

1

I’m following this PDF, I’m on page 10, the code is wrong and I don’t know why.

from tkinter import *
class Janela:
    def __init__(self,toplevel):
        self.fr1 = Frame(toplevel)
        self.fr1.pack()

        self.botao1 = Button(self.fr1,text='Oi!')
        self.botao1['background']='green'
        self.botao1['font']=('Verdana','12','italic','bold')
        self.botao1['height']=3
        self.botao1.pack()

        self.botao2 = Button(self.fr1,bg='red', font=('Times','16'))
        self.botao2['text']='Tchau!'
        self.botao2['fg']='yellow'
        self.botao2['width']=12
        self.botao2.pack()

    raiz=Tk()
    Janela(raiz)
    raiz.mainloop()

1 answer

4


It’s an indentation problem. Python has what we call white space significant, so when Indenta is creating a block. In the document linked the last 3 lines are on the first level, in their code they are at class level, that is, it is calling a code within the class, but it is still being defined. Right:

from tkinter import *
class Janela:
    def __init__(self,toplevel):
        self.fr1 = Frame(toplevel)
        self.fr1.pack()

        self.botao1 = Button(self.fr1,text='Oi!')
        self.botao1['background']='green'
        self.botao1['font']=('Verdana','12','italic','bold')
        self.botao1['height']=3
        self.botao1.pack()

        self.botao2 = Button(self.fr1,bg='red', font=('Times','16'))
        self.botao2['text']='Tchau!'
        self.botao2['fg']='yellow'
        self.botao2['width']=12
        self.botao2.pack()

raiz=Tk()
Janela(raiz)
raiz.mainloop()

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.