简体   繁体   English

__init__() 缺少 1 个必需的位置参数:“接收器”?

[英]__init__() missing 1 required positional argument: 'receiver'?

Im trying to get the data of a word through database with sqlite3 and python but when i try to call the read_from_db function, i have this error _init__() missing 1 required positional argument: 'receiver'.我试图通过使用 sqlite3 和 python 的数据库获取单词的数据,但是当我尝试调用 read_from_db 函数时,出现此错误 _init__() 缺少 1 个必需的位置参数:'receiver'。 I cant seem to find what happened我似乎无法找到发生了什么
heres the code继承人的代码

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

You declare the __init__ method of the class Ui_WordWindow like so:Ui_WordWindow像这样声明Ui_WordWindow类的__init__方法:

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

And it does have a parameter receiver.确实有一个参数接收器。 The error you get indicates that when constructing a Ui_WordWindow you should provide exactly one parameter and that should be the value for receiver.您得到的错误表明,在构造Ui_WordWindow您应该提供一个参数,该参数应该是接收器的值。

Ie this line:即这一行:

window2 = Ui_WordWindow()

Should in fact be:其实应该是:

window2 = Ui_WordWindow(receiver)

where receiver is a valid value for receiver.其中接收器是接收器的有效值。

The receiver object = "some value"接收者对象 =“某个值”

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

you need to brush up on object oriented approach.你需要复习面向对象的方法。 try reading from this: python object oriented尝试从这里阅读: python object oriented

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM