Python __初始化()缺少1个必需的位置参数:';接收器';?

Python __初始化()缺少1个必需的位置参数:';接收器';?,python,sqlite,Python,Sqlite,我试图用sqlite3和python通过数据库获取单词的数据,但是当我试图调用read_from_db函数时,出现了一个错误_init_uuu()缺少1个必需的位置参数:“receiver”。我似乎找不到发生了什么事 这是密码 conn = sqlite3.connect('yeet1.db') cursor = conn.cursor() class Ui_WordWindow(object): def __init__(self, receiver): #Inherit u

我试图用sqlite3和python通过数据库获取单词的数据,但是当我试图调用read_from_db函数时,出现了一个错误_init_uuu()缺少1个必需的位置参数:“receiver”。我似乎找不到发生了什么事
这是密码

conn = sqlite3.connect('yeet1.db')
cursor = conn.cursor()


class Ui_WordWindow(object):


    def __init__(self, receiver):  #Inherit user-input word from another window

        self.receiver = receiver        
        print(self.receiver) #Checking if it worked correctly

    def read_From_db(self): #Read and print out data of user-input word

         cursor.execute(('SELECT * FROM mytable WHERE Meaning = ?', self.receiver))
         data = cursor.fetchall()
         print(data)




window2 = Ui_WordWindow()
window2.read_From_db()
cursor.close()
conn.close

您可以声明类
Ui\u WordWindow
\uuuuu init\uuuuuu
方法,如下所示:

def __init__(self, receiver):  #Inherit user-input word from another window
它有一个参数接收器。您得到的错误表明,在构造
Ui\u WordWindow
时,您应该只提供一个参数,该参数应该是receiver的值

即这一行:

window2 = Ui_WordWindow()
事实上应该是:

window2 = Ui_WordWindow(receiver)
其中,receiver是receiver的有效值。

receiver对象=“一些值”

你需要温习一下面向对象的方法。
试着从以下内容阅读:

当你说window2=Ui\u WordWindow()时,你正在调用构造函数,即init,因此你需要传递对象接收者,就像这个window2=Ui\u WordWindow(接收者)嘿!谢谢你的评论,但在我更改它之后,出现了一个新错误:NameError:name'receiver'未定义你只是将“receiver”作为字符串传递?它应该是实际的接收者对象是的,我将接收者作为字符串传递,有什么问题吗?对不起,我对python很陌生你的接收者应该是某种类型的对象嘿!感谢您的快速回复,我像您所说的那样对其进行了更改,但发生了以下情况:NameError:未定义名称“receiver”,其中receiver是receiver的有效值。这是我回答的一部分。在这里,您应该已经定义了一个变量
receiver
,并且它应该存储一个您称之为receiver的有效实例。感谢您为此投入时间,但如果我这样做,程序将不会自动搜索该词?一旦您阅读了教程,您可能会明白您的错误所在。嘿,我通读了教程,它帮助了我,但我不认为我学到了我想要的。如果我想让receiver成为一个对象,我必须在代码中给它一个ex值(比如你的代码),而不是用户。我想让用户输入的单词本身成为一个对象,这可能吗?
conn = sqlite3.connect('yeet1.db')
cursor = conn.cursor()


class Ui_WordWindow(object):


    def __init__(self, receiver):  #Inherit user-input word from another window

        self.receiver = receiver        
        print(self.receiver) #Checking if it worked correctly
        #when you print self.receiver it means the value which you are passing say for example "test"  

    def read_From_db(self): #Read and print out data of user-input word

         cursor.execute(('SELECT * FROM mytable WHERE Meaning = ?', "test"))

         # now the query becomes select * from mytable where meaning = 'test'
         # the value 'test' is being passed by the receiver object and you need to provide that value

         data = cursor.fetchall()
         print(data)




window2 = Ui_WordWindow(receiver) # which has some value ex 'test'
window2.read_From_db()
cursor.close()
conn.close