3

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()
Zoe
  • 27,060
  • 21
  • 118
  • 148
mikkolo
  • 39
  • 4

4 Answers4

3

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.

kabanus
  • 24,623
  • 6
  • 41
  • 74
  • Dropping the `(self)` worked. Strangely, I need to have it as an argument when I trigger the function by pressing `Enter`, coded with `my_tk_entry_widget.bind('', my_function)`. I made this a new Q/A at [Either Enter or button trigger "TypeError ... missing 1 required positional argument" or "TypeError ... takes 0 positional arguments but 1 was given"](https://stackoverflow.com/questions/76276568/either-enter-or-button-trigger-typeerror-missing-1-required-positional-argu/76276569#76276569). – questionto42 May 17 '23 at 23:36
2

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

class view():

    ...

    def button_click(self):
        self.form.destroy()
        Chatclient().design()
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38
1

You need to put the function definition for button_click() inside the class.

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()

Basically, you need to indent the code for the function definition.

Actually, when you place the code for function inside the class then it becomes a member function for that class and by passing the argument self, you just actually use a reference to the object(instance of the class) for which that function is called. It's like this in C++ if you know about that.

You can read more about self here.

Abhishek Arya
  • 450
  • 4
  • 16
0

You also need to indent the last two button lines. I checked your code by commenting out anything that I could not test:

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.form.destroy()
        print('test')
        # Chatclient().design()

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

root.mainloop()

Out:

enter image description here

questionto42
  • 7,175
  • 4
  • 57
  • 90