S
S
shutov_952019-06-16 20:50:36
Python
shutov_95, 2019-06-16 20:50:36

How to code dynamic (by button) change of table properties in PyQT5?

Given: 2 QLineEdits and 1 button.
Necessary:
​​1) It is necessary to read the data from QLineEdit'ov by pressing the button, cast it to the int type (because I suspect that they are considered in str) and push it into the table.setRowCount() and table.setColumnCount() commands. That is, change the appearance of the table based on the entered data.
Desirable:
2) Explain to the negligent author of the note information about the creation of "crooked" tables. That is, in the first column of such a table there will be, for example, 5 rows. In the second column, each of the rows of the first column will correspond to a certain number of rows (i.e., the first row of the first column will have one field, and the first row of the second column will have 5 or 3 of them). Approximately the same in relation to the 3rd column relative to the 2nd column. That is, if you go in reverse order and draw a parallel with Excel, this is the combination of several cells into one.
In the code, an attempt was made to create (NOT show) a table by pressing a key (which, as I understand it, is either very difficult or almost impossible), so please do not pay attention to this.

Under the cut is the code.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QLabel, QWidget, QMessageBox, QApplication, QToolTip, QPushButton, QHBoxLayout, QVBoxLayout, QLineEdit, QDesktopWidget, QTableWidget
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import pyqtSlot


class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # задаём параметры окна, имя, иконку в корне
        self.resize(730, 486)
        self.setWindowTitle('Message box')
        self.setWindowIcon(QIcon('icon.png'))

        # определяем лейблы с расположением и размерами
        l1 = QLabel('Количество экспертов', self)
        l1.setGeometry(10, 0, 121, 21)
        l2 = QLabel('Количество кандидатов', self)
        l2.setGeometry(10, 60, 131, 16)

        # определяем textbox'ы с расположением и размерами
        txb1 = QLineEdit(self)
        txb1.setGeometry(10, 30, 113, 20)
        txb2 = QLineEdit(self)
        txb2.setGeometry(10, 80, 113, 20)

        # определяем кнопку, её подсказку, размер и прочее
        btn1 = QPushButton('Сфомировать таблицу', self)
        btn1.setToolTip('This is a <b>QPushButton</b> widget')
        btn1.setGeometry(140, 80, 131, 23)

        #определяем ещё не видную таблицу
        t1 = QTableWidget(self)

        btn1.clicked.connect(self.on_click)
        self.show()

    # происходит троллинг (нажатие клавиши)
    @pyqtSlot()
    def on_click(self):
        txbVal1 = self.txb1.text()
        txbVal2 = self.txb2.text()
        t1.setRowCount(self, txbVal1)
        t1.setColumnCount(self, txbVal2)
        t1.SetGeomety(10, 130)
        self.t1.show()


    def closeEvent(self, event):

        reply = QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QMessageBox.Yes |
            QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question