简体   繁体   English

PyQt4从另一个类调用方法

[英]PyQt4 calling Method from another Class

I got Class A with the method i want to call. 我使用要调用的方法获得了A类。

class Class_A(QtGui.QMainWindow):
    def __init__(self, database, tableName):
        QtGui.QWidget.__init__(self)
        self.dbu = database_manager_2.DatabaseUtility(database, tableName)
        self.setupUi(self)

    def setupUi(self, Class_A):
         ...

    def print_somethig (self):
           print 'hello' 

This is class B : 这是B类:

class class B(object):              
    def setupUi_1(self, Dialog):
        ...
        self.my_instance = Class_A()
        QtCore.QObject.connect(self.pushButtonSecond, QtCore.SIGNAL(_fromUtf8("clicked()")),self.my_instance.print_something() )

As you can see i have created an instance from class A so i can call it's method in class B. 如您所见,我已经从类A创建了一个实例,因此我可以在类B中调用它的方法。

I got this error : 我收到此错误:

TypeError: __init__() takes exactly 3 arguments (1 given) TypeError: __init__()恰好接受3个参数(给定1个)

I know this is something related to OOP. 我知道这与OOP有关。

That's nothing related with OOP: 与OOP无关:

Your method signature is: 您的方法签名是:

def __init__(self, database, tableName):

So if you call: a = Class_A() you give just 1 parameter (self is implicitly given as a parameter). 因此,如果您调用: a = Class_A()给出1个参数(self隐式作为参数给出)。 There is no to overcome it. 没有什么可以克服的。 You could define default values like that: 您可以这样定义默认值:

def __init__(self, database=None, tableName=None):

So if you call: 因此,如果您致电:

a = Class_A()

database and tableName will be None. database和tableName将为None。 But then: 但是之后:

self.dbu = database_manager_2.DatabaseUtility(database, tableName)

throws an error I think. 我认为会引发错误。

But with your original method signature, there is no way around that. 但是,如果使用原始方法签名,则无法解决。

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

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