0

I have 2 python files, from one file want to call and run a QDialog with Qlistview. I want to contruct in pythonic way getting value of index just before terminating first python file.

Structure of files:

---Widgetclass.py
---Class_test.py

The code:

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_())

The constructor:

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_()

Error getting:

AttributeError: type object 'Widget' has no attribute '_INDEX'

I want to achieve value and close the file. but seems not working?!

Pavel.D
  • 561
  • 1
  • 15
  • 41
  • 1) change `ls._INDEX` and `ls.close()` to `subwindow._INDEX` and `subwindow.close()`, respectively. 2) add `import sys` `app = QtWidgets.QApplication(sys.argv)` before `subwindow=ls()` – eyllanesc Jul 15 '19 at 21:38
  • I recommend reading a lot about OOP so that you understand that it is an attribute of an object and attribute of a class, which is itself an object and a class. – eyllanesc Jul 15 '19 at 21:40
  • Well, thanks it works fine.even without 'app = QtWidgets.QApplication(sys.argv)'. It seems need of knowledge about basic OOP class. I take your recommendation into account. – Pavel.D Jul 15 '19 at 22:18

0 Answers0