在 PyQt 中将变量从一个 Class 传递到另一个
Pass a variable from one Class to another in PyQt
我想在 PyQt 中将字符串变量从 Main_Window
class 传递到另一个 QDialog
class。我不明白我做错了什么。我想将 host_mac
变量从 Main Class 传递给 QDialog Class。这是我的代码的主要部分。
这是 QDialog class:
class Client(QDialog):
def __init__(self, parent=None):
super(Client, self).__init__(parent)
self.pb = QPushButton()
self.pb.setObjectName("connect")
self.pb.setText("Connect")
layout = QFormLayout()
layout.addWidget(self.pb)
self.setLayout(layout)
self.connect(self.pb, SIGNAL("clicked()"),self.set_client)
self.setWindowTitle("Learning")
def set_client(self):
self.client = self.le.text()
print 'provided client mac is ' + self.client + 'and host_mac is ' + Main_window_ex.host_mac
这里是 Main_Window Class:
class Main_window_ex(QMainWindow, Ui_Main_window):
def __init__(self, parent = None):
"""
Default Constructor. It can receive a top window as parent.
"""
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.host_mac = 'blah blah'
#more code beneeth
但我收到以下错误:
AttributeError: type object 'Main_window_ex' has no attribute 'host_mac'
Main_window_ex.host_mac
指的是一个 class 变量(因为 Main_window_ex
只是一个 class),但是你想要访问 instance 变量。换句话说, host_mac
直到 class 被实例化后才被定义。
有几种解决方法。假设 Main_window_ex
负责创建 Client
,那么一种简单的方法是将变量传递给 Client
:
class Client(QDialog):
def __init__(self, host_mac, parent=None):
self.host_mac = host_mac
...
并像这样使用它:
def set_client(self):
self.client = self.le.text()
print 'provided client mac is ' + self.client + 'and host_mac is ' + self.host_mac
作为旁注,您可能希望使用新样式的连接语法:
# old style
# self.connect(self.pb, SIGNAL("clicked()"),self.set_client)
# new style
self.pb.clicked.connect(self.set_client)
我想在 PyQt 中将字符串变量从 Main_Window
class 传递到另一个 QDialog
class。我不明白我做错了什么。我想将 host_mac
变量从 Main Class 传递给 QDialog Class。这是我的代码的主要部分。
这是 QDialog class:
class Client(QDialog):
def __init__(self, parent=None):
super(Client, self).__init__(parent)
self.pb = QPushButton()
self.pb.setObjectName("connect")
self.pb.setText("Connect")
layout = QFormLayout()
layout.addWidget(self.pb)
self.setLayout(layout)
self.connect(self.pb, SIGNAL("clicked()"),self.set_client)
self.setWindowTitle("Learning")
def set_client(self):
self.client = self.le.text()
print 'provided client mac is ' + self.client + 'and host_mac is ' + Main_window_ex.host_mac
这里是 Main_Window Class:
class Main_window_ex(QMainWindow, Ui_Main_window):
def __init__(self, parent = None):
"""
Default Constructor. It can receive a top window as parent.
"""
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.host_mac = 'blah blah'
#more code beneeth
但我收到以下错误:
AttributeError: type object 'Main_window_ex' has no attribute 'host_mac'
Main_window_ex.host_mac
指的是一个 class 变量(因为 Main_window_ex
只是一个 class),但是你想要访问 instance 变量。换句话说, host_mac
直到 class 被实例化后才被定义。
有几种解决方法。假设 Main_window_ex
负责创建 Client
,那么一种简单的方法是将变量传递给 Client
:
class Client(QDialog):
def __init__(self, host_mac, parent=None):
self.host_mac = host_mac
...
并像这样使用它:
def set_client(self):
self.client = self.le.text()
print 'provided client mac is ' + self.client + 'and host_mac is ' + self.host_mac
作为旁注,您可能希望使用新样式的连接语法:
# old style
# self.connect(self.pb, SIGNAL("clicked()"),self.set_client)
# new style
self.pb.clicked.connect(self.set_client)