Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:button_click()缺少1个必需的位置参数:';自我';_Python_Python 3.x_Tkinter_Arguments_Typeerror - Fatal编程技术网

Python TypeError:button_click()缺少1个必需的位置参数:';自我';

Python TypeError:button_click()缺少1个必需的位置参数:';自我';,python,python-3.x,tkinter,arguments,typeerror,Python,Python 3.x,Tkinter,Arguments,Typeerror,我总是不断收到一个类型错误,说我缺少一个必需的位置参数,即“self”,我该如何解决这个问题 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(

我总是不断收到一个类型错误,说我缺少一个必需的位置参数,即“self”,我该如何解决这个问题

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

按钮\u单击类
视图中的
方法

问题在于:

button = Button(root, text="Create Group Chat", command= button_click)
注意这个命令-它说要调用
按钮(单击
),并且将不带任何参数。您将单击功能定义为

def button_click(self):
因此,当您单击按钮并调用
按钮时,请单击不带参数的
,因为您的定义需要一个自参数-无论是因为它在类中还是出于任何原因-您都会得到错误。要么去掉参数中的
self

def button_click():
或者,如果它应该是类定义的一部分,则只使用有效对象定义按钮。例如,您可以在
def\uuuu init\uuuu(self)
中放入:


另外,在构造函数中构造GUI是一种很好的设计。

您需要将button_click()的函数定义放在类中

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()
基本上,您需要缩进函数定义的代码

实际上,当您将函数的代码放在类中时,它将成为该类的成员函数,并且通过传递参数self,您实际上只是使用对调用该函数的对象(类的实例)的引用。如果你知道,就像C++中的代码< <代码> >
您可以阅读更多有关self的信息。

单击按钮是否应该缩进类中?
self.button = Button(root, text="Create Group Chat", command= self.button_click)
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()