Error: name 'self' is not defined

Asked

Viewed 35 times

0

I’m a beginner in Python and I’m developing my first program. It’s like this:

from tkinter import *
from tkinter import font

class Application(Frame):
 def __init__(self, master=None):
 pass
 self.msg = Label(self, text="Bem vindos a GUIDA!!", fg = 'blue', font = 'Candara')
self.msg.place(x=50,y=70)
self.bye = Button (self, text="Bye", command=self.quit)
self.bye.pack ()
self.pack()
self.msg_2 = Label(self, text="Selecione os tópicos e exclareça suas dúvidas!", fg = 'black', font = 'Candara')
self.msg_2.place(x=80,y=100)
self.view = Button (self, text="Ver Tópicos", font = "Algerian", command = comando, fg = 'black', bg = 'white', activebackground = 'white', activeforeground = 'yellow')
self.view.place(x=400,y=100)

root = Tk()

root["bg"] = "green"

root.title('GUIDA')

root.geometry('750x700')

app = Application()

mainloop()

But when I run, it opens like this:

inserir a descrição da imagem aqui

I wanted to know how to solve this mistake.

  • Avoid posting your codes in image, edit your question and place them here

  • 1

    Please, do not post your code as image, the site has support for formatting it; you can get more information by doing the [tour], reading the [Ask] tab and accessing [help]. The error in question is because you are switching to the initializer of Label a variable self that does not exist and it seems to be wrong even if it existed. The parameter self is implicitly defined by language when you instantiate a class, you hardly need to define it directly.

  • I do @Andersoncarloswoss?

  • Start by studying and understanding what is and how self. In the link I marked there is a good material for you to start studies.

  • Lima, Cara by what it looks like here is just your identation, when you use the self it is not inside the method block init Try correcting the identation and see if it works. According to PEP8 python works with 4 indentation spaces, and if you do not respect indentation your code will fail.

No answers

Browser other questions tagged

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