Home > OS >  Tkinter: missing 1 required positional argument: 'self'
Tkinter: missing 1 required positional argument: 'self'

Time:12-26

thanks in advance for your help. I'm learning about Tkinter and that's why I tried to write a simple code for a window where when I type a number and click on the ("trocar cor de fundo") or ("change background color") button, the background color of the window background changes. Well I'm having an error and I can't figure out what I did wrong. If anyone can help me I would be very grateful.

error

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Alan\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
TypeError: change_color() missing 1 required positional argument: 'self'
from tkinter import *
from tkinter import messagebox
import os

class MinhaJanela:
    def __init__(self, janela):
        self.texto_01 = Label(janela, text="Escolha uma cor: 01 / 02 / 03", 
                              font="Lucida 20 bold", 
                              bg="#363636", fg="white")
        self.entrada = Entry(janela, bd=3,
                             width=35,
                             justify=LEFT)
        self.botao_01 = Button(janela, text="Trocar Cor de Fundo",
                               font="Helvetica 14 bold",
                               command=change_color)
        self.botao_02 = Button(janela, text="Sair",
                               font="Arial 14 bold",
                               command=sair)
        self.texto_01.place(x=55, y=30)
        self.entrada.place(x=140, y=80, height=30)
        self.botao_01.place(x=140, y=120)
        self.botao_02.place(x=220, y=165, height=30)

    

def change_color(self):
    cor = (self.entrada.get())
    if cor == "1" or cor == "01":
        janela.config(self, bg="green")
    if cor == "2" or cor == "02":
        janela.config(bg="red")
    if cor == "3" or cor == "03":
        janela.config(bg="yellow")
    else:
        messagebox.showerror("Escolha uma cor entre 01 e 03")
    
def sair():
    os._exit(0)


janela = Tk()
janela.wm_attributes("-transparentcolor", "grey")
minhajanela = MinhaJanela(janela)
janela.title("Teste de janela com widget")
janela.resizable(False, False)
janela.configure(bg="#363636")

largura = 500
altura = 300

largura_do_monitor = janela.winfo_screenwidth()
altura_do_monitor = janela.winfo_screenheight()

posx = largura_do_monitor / 2 - largura / 2
posy = altura_do_monitor / 2 - altura / 2
janela.geometry("%dx%d %d %d" % (largura, altura, posx, posy))

janela.mainloop()

CodePudding user response:

I believe that this is because change_color is not added to the class. To fix it, simply indent the function so that it will be added to the class.

Also there's a small error when you call config - you don't need to pass the self argument.

Working code:

from tkinter import *
from tkinter import messagebox
import os

class MinhaJanela:
    def __init__(self, janela):
        self.texto_01 = Label(janela, text="Escolha uma cor: 01 / 02 / 03",
                              font="Lucida 20 bold",
                              bg="#363636", fg="white")
        self.entrada = Entry(janela, bd=3,
                             width=35,
                             justify=LEFT)
        self.botao_01 = Button(janela, text="Trocar Cor de Fundo",
                               font="Helvetica 14 bold",
                               command=self.change_color)  # Change to self.change_color
        self.botao_02 = Button(janela, text="Sair",
                               font="Arial 14 bold",
                               command=sair)
        self.texto_01.place(x=55, y=30)
        self.entrada.place(x=140, y=80, height=30)
        self.botao_01.place(x=140, y=120)
        self.botao_02.place(x=220, y=165, height=30)

    def change_color(self):  # Indented
        cor = (self.entrada.get())
        if cor == "1" or cor == "01":
            janela.config(bg="green")  # No self here
        if cor == "2" or cor == "02":
            janela.config(bg="red")
        if cor == "3" or cor == "03":
            janela.config(bg="yellow")
        else:
            messagebox.showerror("Escolha uma cor entre 01 e 03")

def sair():
    os._exit(0)


janela = Tk()
janela.wm_attributes("-transparentcolor", "grey")
minhajanela = MinhaJanela(janela)
janela.title("Teste de janela com widget")
janela.resizable(False, False)
janela.configure(bg="#363636")

largura = 500
altura = 300

largura_do_monitor = janela.winfo_screenwidth()
altura_do_monitor = janela.winfo_screenheight()

posx = largura_do_monitor / 2 - largura / 2
posy = altura_do_monitor / 2 - altura / 2
janela.geometry("%dx%d %d %d" % (largura, altura, posx, posy))

janela.mainloop()
  • Related