TypeError: button_click() missing 1 required positional argument: 'self'
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.
put button_click
method inside the class view
, some explication about self
class view(): ... def button_click(self): self.form.destroy() Chatclient().design()
You need to put the function definition for button_click() inside the class.
from tkinter import *import tkinterfrom 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.