我有2个python文件,其中一个要调用并使用Qlistview运行QDialog。我想以pythonic的方式构造,以便在终止第一个python文件之前获得索引的值。
文件结构:
---Widgetclass.py
---Class_test.py
代码:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
self.button = QtWidgets.QPushButton("Okay")
self.listView = QtWidgets.QListView()
lay.addWidget(self.listView)
self.entry = QtGui.QStandardItemModel()
self.listView.setModel(self.entry)
self.listView.setSpacing(5)
for text in ("One", "two", "Three", "Four",
"Five etc.."):
it = QtGui.QStandardItem(text)
self.entry.appendRow(it)
Code_Group = QtWidgets.QGroupBox(self)
Code_Group.setTitle("&Test")
Code_Group.setLayout(lay)
Vlay = QtWidgets.QVBoxLayout(self)
Vlay.addWidget(Code_Group)
Vlay.addWidget(self.button, alignment=QtCore.Qt.AlignCenter)
Vlay.setSizeConstraint(Vlay.SetFixedSize)
self.listView.selectionModel().currentChanged.connect(self.on_row_changed)
self._INDEX = 0
def on_row_changed(self, current, previous):
self._INDEX = current.row()
print('Row %d selected' % current.row())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
构造函数:
from Widgetclass import Widget as ls
from PyQt5 import QtCore, QtGui, QtWidgets
def value():
print('Row index:', ls._INDEX, ':') #<---- Value not callable?
ls.close()
subwindow=ls()
subwindow.setWindowModality(QtCore.Qt.ApplicationModal)
subwindow.button.clicked.connect(value)
subwindow.exec_()
获取错误:
AttributeError: type object 'Widget' has no attribute '_INDEX'
我想实现价值并关闭文件。但似乎不起作用?!