U
U
Ulyana Illiterate2016-12-16 11:11:01
Python
Ulyana Illiterate, 2016-12-16 11:11:01

Python - how to implement OOP with PyQt (connect)?

There is a window in PyQt in it, the Open button for loading a file
. In the same window, there is a table that is filled with the contents of the file.
I can’t correctly connect everything (
Here are the parts of the program:

class open_kn(QPushButton):
    def __init__(self, parent=None):
        super(open_kn, self).__init__(parent)
        self.setText('Open')
        self.resize(self.sizeHint())
        self.clicked.connect(FileDialog)
        

class FileDialog(QFileDialog):
    def __init__(self):
        super().__init__()
        try:
            self.fileName = self.getOpenFileName(self, 'Open file', 'D:\\')[0]
            #self.textEdit.setText(self.fileName)
            f = open(self.fileName, 'r')
            data = []
            with f as f:
                for i in f.readlines():
                    data.append(i)
            TableWidget.data = data
        except IOError:
            print('Не могу открыть', self.fileName)
        except ImportError as err:
            print('Error: импортировать файл не удалось!', err)
        except ValueError as err:
            print('Error: не тот формат данных!', err)
        except:
            print('Неожиданная ошибка:', sys.exc_info()[0])


class TableWidget(QTableWidget):
    def __init__(self, parent=None, data=[]):
        super(TableWidget, self).__init__(parent)
        #data = []
        self.setColumnCount(1)
        self.setRowCount(len(data))
        for i, entry in enumerate(data, start=1):
            self.setRowCount(i)
            item = QTableWidgetItem()
            item.setText(str(entry))

The table itself is inserted by QGridLayout:
I thought I could set the data from the file to the variable and there it’s just to declare the table with the attribute,
but I don’t know how to implement it
Here is the class
class HBox4(QGridLayout):
    def __init__(self, parent=None):
        super(HBox4, self).__init__(parent)
        table = TableWidget()
        self.addWidget(table, *(0,0))
        self.addWidget(TabDemo(), *(0,1))

I just started learning programming, don't judge strictly
I think maybe you shouldn't do it at all(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mindlin, 2016-12-16
@karulyana

Make an update method in the HBox4 class:

class HBox4(QGridLayout):
    def __init__(self, parent=None):
        super(HBox4, self).__init__(parent)
        self.table = TableWidget()
        self.addWidget(self.table, *(0,0))
        self.addWidget(TabDemo(), *(0,1))
    def update(self,data):
        self.table.update(data)

Well, in the Table class the same thing:
class TableWidget(QTableWidget):
    def __init__(self, parent=None, data=[]):
        super(TableWidget, self).__init__(parent)
        #data = []
        self.setColumnCount(1)
        self.setRowCount(len(data))
        for i, entry in enumerate(data, start=1):
            self.setRowCount(i)
            item = QTableWidgetItem()
            item.setText(str(entry))
   def update(self,data):
        self.clearContents ()
        #тут очищаем таблицу, затем тот же код, что и в init
        self.setColumnCount(1)
        self.setRowCount(len(data))
        for i, entry in enumerate(data, start=1):
            self.setRowCount(i)
            item = QTableWidgetItem()
            item.setText(str(entry))

Something like this....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question