Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: button_click() missing 1 required positional argument: 'self'

I always keep on getting a type error saying that I am missing 1 required positional argument which is the 'self' how can I fix this?

from tkinter import *
import tkinter
from client import*

root = tkinter.Tk()
class view():    
    root.geometry("250x300")
    F1 =Frame()
    L = Listbox(F1)
    L.grid(row=0, column =0) 

    L.pack()

    F = open("users.txt","r")
    M = F.read()
    cont = M.split()

    for each in cont:
        ind = each.find("#") + 1
        L.insert(ind+1 ,each[ind:])
        break

    F.close()

    F1.pack()

    # strng_ind = -1
def button_click(self):
        self.form.destroy()
        Chatclient().design()

button = Button(root, text="Create Group Chat", command= button_click)

button.pack()
root.mainloop()
like image 745
mikkolo Avatar asked May 05 '26 03:05

mikkolo


2 Answers

The problem is here:

button = Button(root, text="Create Group Chat", command= button_click)

Note the command - it says to invoke button_click, and will with no arguments. You defined the click function as

def button_click(self):

so when you click the button and invoke button_click with no arguments, since your definition requires a self argument - whether if it's because it's in a class or for whatever reason - you get the error. Either get rid of self in the arguments

def button_click():

or if it's supposed to be a part of the class definition, only define the Button with a valid object. For example, you can put inside def __init__(self):

self.button = Button(root, text="Create Group Chat", command= self.button_click)

with the added bonus of constructing your GUI in the constructor, which is good design.

like image 151
kabanus Avatar answered May 06 '26 16:05

kabanus


put button_click method inside the class view, some explication about self

class view():

    ...

    def button_click(self):
        self.form.destroy()
        Chatclient().design()
like image 39
Druta Ruslan Avatar answered May 06 '26 15:05

Druta Ruslan



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!