我正在使用PyQt
來創建一個matplotlib
的應用程序,我試圖從一個類與另一個類的另一個方法連接一個方法。從python裏面的方法連接三個類
A類是一個QMainWindow
,它實例化了class B
,這是一個matplotlib
圖。所以,當我從class A
(連接到QPushButton
)調用方法a
時,它調用class C
做某事,然後它必須連接到 class B
做其他事情。
我讀了很多編碼,我發現這個例如:
python super calling child methods
,我試圖這樣做,但沒有奏效。
這是我的代碼:
class A(QMainWindow):
def __init__(self):
super(QMainWindow, self).__init__()
#A lot of stuff in here
#I instantiate class B
self.call_B = B()
def a(self):
print "Hello"
self.open_C = C(self)
class B(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
#A lot of stuff in here
def b(self):
print "How are you?"
class C(B):
def __init__(self):
super(C, self).__init__()
#A lot of stuff in here
self.connect_to_b()
def connect_to_b(self):
#From here is where I need to call method b() from class B
正如你可以在上面看到,我需要從class C
在class B
連接方法connect_to_b
到方法b
。
我該如何做到這一點?希望您能夠幫助我。
如果'C'類型爲'B',則它可以訪問'b'。只需用'b()'調用'b'' –
謝謝您的評論。我嘗試過,但沒有奏效。也許我錯過了一些東西。我得到這個錯誤,而這樣做:'全球名稱'b'未定義' –
使用'self.b()'因爲'b'在你的班級 –