【发布时间】:2013-11-25 22:48:52
【问题描述】:
我正在用 Python 编写一个 GUI 应用程序,它使用多个 .py 脚本。我在 QMainWindow 中有一个变量,我需要在其他类中引用/访问它。将各种 .py 模块导入 Ui_MainWindow.py 模块没有问题,但我似乎无法访问 QMainWindow 类变量。
这是我正在尝试的快速伪代码:
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.lineEditScanBarcode.returnPressed.connect(self.LoginAttempt)
def LoginAttempt(self):
self.user_barcode = self.lineEditScanBarcode.text()
根据我对类变量的阅读,我得出的结论是,通过上述设置,我应该能够引用其他类中的“user_barcode”变量,如下所示:
class Receipt(QWidget, Ui_Receipt):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setupUi(self)
print(MainWindow.user_barcode)
我一直在使用“打印”命令来测试它是否有效,但我收到以下错误:
Attribute Error: type object 'MainWindow' has no attribute 'user_barcode'
谁能看到我明显犯的错误?我搜索了类似的查询,但没有找到任何相关的内容。
谢谢!
编辑:
这是 app.exec_() 设置,我不确定我是否正确传递了父级。
if __name__ == '__main__':
app = QApplication(sys.argv)
showMainWindow = MainWindow()
showReceipt = Receipt(MainWindow)
showMainWindow.show()
app.exec_()
我尝试了各种组合,但要么收到 init 错误,要么收到引发的 TypeError。
【问题讨论】:
标签: python pyqt pyside qmainwindow