Answer the question
In order to leave comments, you need to log in
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))
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))
Answer the question
In order to leave comments, you need to log in
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)
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))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question