我想将一个字符串变量从Main_Window
类传递给PyQt中的另一个QDialog
类。我不明白我做错了什么。我想把host_mac
变量从主类传递到QDialog类。这里是我代码的主要部分。在
以下是QDialog类:
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
这里是主窗口类:
^{pr2}$但我得到了以下错误:
AttributeError: type object 'Main_window_ex' has no attribute 'host_mac'
Main_window_ex.host_mac
引用一个类变量(因为Main_window_ex
只是一个类),但是您想要访问实例变量。换句话说,host_mac
直到类实例化后才被定义。在有几种方法可以解决这个问题。假设
Main_window_ex
负责创建Client
,那么一种简单的方法是将变量传递给Client
:使用方式如下:
^{pr2}$另外,您可能需要使用新样式的连接语法:
相关问题 更多 >
编程相关推荐