NameError: name 'Window' is not defined [closed]

1

I'm following this PDF , I'm on page 10, the code gives error and I I do not 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()
    
asked by anonymous 28.11.2018 / 13:44

1 answer

4

It's an indentation problem. Python has what we call meaningful white space, so when indent is creating a block. In the linked document the last 3 lines are in the first level, in your code they are at the class level, ie you are calling a code inside the class, but it is still being defined. The correct one:

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 placed GitHub for future reference .     

28.11.2018 / 13:56