Home>

There are multiple tablestableWidget_1,tableWidget_2,tableWidget_3, ..etc.

There is a code like this:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget, QTableWidgetItem, QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
# okk comes from the server via socket
okk= [(17, 0, 0, 0, 0, '2021-10-16', 0),
        (16, 22, 22, 33, 44, '2021-10-14', 55),
        (15, 22, 33, 44, 1, '2021-10-14', 4),
        (14, 987, 987, 987, 987, '2021-10-14', 987),
        (13, 11, 22, 33, 44, '2021-10-14', 55),
        (12, 346, 354, 6346, 2324, '2021-10-11', 34346),
        (11, 25, 634, 364, 235, '2021-10-11', 634),
        (10, 534, 63, 436, 754, '2021-10-11', 364),
        (9, 643, 345, 2356, 34, '2021-10-11', 643),
        (8, 345, 634, 364, 634, '2021-10-11', 643),
        (7, 3425, 435, 53, 35, '2021-10-11', 534),
        (6, 63, 7865, 123, 5632456, '2021-10-11', 230),
        (5, 11, 22, 33, 44, '2021-10-11', 55),
        (4, 1, 2, 3, 4, '2021-10-11', 5),
        (3, 22, 22, 222, 22, '2021-10-10', 222),
        (2, 777, 777, 777, 777, '2021-10-10', 777),
        (1, 22, 33, 44, 55, '2021-10-10', 66)]
# command mask for the server and table processing
base= [{'message': 'zavod_1_92_25', 'id': 1},
 {'message': 'zavod_2_92_25', 'id': 2}]
class App (QWidget):
    def __init __ (self):
        super () .__ init __ ()
        self.title= 'EXAMPLE EXAMPLE OF TABLES'
        self.left= 300
        self.top= 300
        self.width= 1000
        self.height= 800
        self.initUI ()
    def initUI (self):
        self.setWindowTitle (self.title)
        self.setGeometry (self.left, self.top, self.width, self.height)
        self.myAddInTableS ()
        self.layout= QVBoxLayout ()
        self.layout.addWidget (self.tableWidget_1)
        self.layout.addWidget (self.tableWidget_2)
        self.setLayout (self.layout)
        self.show ()
    def myAddInTableS (self):
        self.tableWidget_1= QTableWidget ()
        self.tableWidget_1.setRowCount (4)
        self.tableWidget_1.setColumnCount (7)
        self.tableWidget_2= QTableWidget ()
        self.tableWidget_2.setRowCount (4)
        self.tableWidget_2.setColumnCount (7)
        for item in base:
            message= item ['message']
            id_table= item ['id']
            print ('Sending a command to the server side to trigger the request and send back OKK variable:', message)
            print ('id_table:', id_table, '-This value should change the table number, but I don’t know how to do it. POMMO.G.I.T.E.!')
            self.tableWidget_1.setRowCount (len (okk))
            tablerow= 0
            for row in okk:
                self.tableWidget_1.setItem (tablerow, 0, QTableWidgetItem (str (row [0])))
                self.tableWidget_1.setItem (tablerow, 1, QTableWidgetItem (str (row [1])))
                self.tableWidget_1.setItem (tablerow, 2, QTableWidgetItem (str (row [2])))
                self.tableWidget_1.setItem (tablerow, 3, QTableWidgetItem (str (row [3])))
                self.tableWidget_1.setItem (tablerow, 4, QTableWidgetItem (str (row [4])))
                self.tableWidget_1.setItem (tablerow, 5, QTableWidgetItem (row [5]))
                self.tableWidget_1.setItem (tablerow, 6, QTableWidgetItem (str (row [6])))
                tablerow += 1
if __name__== '__main__':
    app= QApplication (sys.argv)
    ex= App ()
    sys.exit (app.exec_ ())

In my understandingID_TABLEthis should be the number of the table where to push the data during the next iteration of the loop.

How to make a replacementtableWidget_1ontableWidget_2when going through a loop iteration?

I want the most concise code.

Guys, please help me.

  • Answer # 1

    Slightly shorter and more versatile:

    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, \
        QAction, QTableWidget, QTableWidgetItem, QVBoxLayout, qApp
    class MainWindow (QWidget):
        def __init __ (self, okk, base):
            super () .__ init __ ()
            self.okk= okk
            self.base= base
            self.tableWidgets= []
            self.layout= QVBoxLayout (self)
            self.myAddInTableS ()
        def myAddInTableS (self):
            for i, item in enumerate (self.base):
                message= item ['message']
                id_table= item ['id']
                print (f'Send a command to the server side: id= {id_table}, message= {message} ')
                certainTable= QTableWidget (0, 7, objectName= f'tableWidget_ {i + 1} ')
                certainTable.setHorizontalHeaderLabels (['Hello', certainTable.objectName (),])
                for row, data in enumerate (self.okk):
                    _data= list (map (str, data))
                    certainTable.insertRow (row)
                    for col, item in enumerate (_data):
                        certainTable.setItem (row, col, QTableWidgetItem (item))
                self.layout.addWidget (certainTable)
                self.tableWidgets.append (certainTable)
                qApp.processEvents ()
    okk= [
        (17, 0, 0, 0, 0, '2021-10-16', 0),
        (16, 22, 22, 33, 44, '2021-10-14', 55),
        (15, 22, 33, 44, 1, '2021-10-14', 4),
        (14, 987, 987, 987, 987, '2021-10-14', 987),
        (13, 11, 22, 33, 44, '2021-10-14', 55),
        (12, 346, 354, 6346, 2324, '2021-10-11', 34346),
        (11, 25, 634, 364, 235, '2021-10-11', 634),
        (10, 534, 63, 436, 754, '2021-10-11', 364),
        (9, 643, 345, 2356, 34, '2021-10-11', 643),
        (8, 345, 634, 364, 634, '2021-10-11', 643),
        (7, 3425, 435, 53, 35, '2021-10-11', 534),
        (6, 63, 7865, 123, 5632456, '2021-10-11', 230),
        (5, 11, 22, 33, 44, '2021-10-11', 55),
        (4, 1, 2, 3, 4, '2021-10-11', 5),
        (3, 22, 22, 222, 22, '2021-10-10', 222),
        (2, 777, 777, 777, 777, '2021-10-10', 777),
        (1, 22, 33, 44, 55, '2021-10-10', 66)
    ]
    base= [
        {'message': 'zavod_1_92_25', 'id': 1},
        {'message': 'zavod_2_92_25', 'id': 2}
    ]
    if __name__== '__main__':
        app= QApplication (sys.argv)
        ex= MainWindow (okk, base)
        ex.setWindowTitle ('SAMPLE EXAMPLE OF TABLES')
        ex.resize (770, 500)
        ex.show ()
        sys.exit (app.exec_ ())
    

    Here, thanks for that)

    Yan Wolf2021-10-19 03:30:11

    @YanWolf please

    S. Nick2021-10-19 03:30:11
  • Answer # 2

    Slightly shorter and more versatile:

    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, \
        QAction, QTableWidget, QTableWidgetItem, QVBoxLayout, qApp
    class MainWindow (QWidget):
        def __init __ (self, okk, base):
            super () .__ init __ ()
            self.okk= okk
            self.base= base
            self.tableWidgets= []
            self.layout= QVBoxLayout (self)
            self.myAddInTableS ()
        def myAddInTableS (self):
            for i, item in enumerate (self.base):
                message= item ['message']
                id_table= item ['id']
                print (f'Send a command to the server side: id= {id_table}, message= {message} ')
                certainTable= QTableWidget (0, 7, objectName= f'tableWidget_ {i + 1} ')
                certainTable.setHorizontalHeaderLabels (['Hello', certainTable.objectName (),])
                for row, data in enumerate (self.okk):
                    _data= list (map (str, data))
                    certainTable.insertRow (row)
                    for col, item in enumerate (_data):
                        certainTable.setItem (row, col, QTableWidgetItem (item))
                self.layout.addWidget (certainTable)
                self.tableWidgets.append (certainTable)
                qApp.processEvents ()
    okk= [
        (17, 0, 0, 0, 0, '2021-10-16', 0),
        (16, 22, 22, 33, 44, '2021-10-14', 55),
        (15, 22, 33, 44, 1, '2021-10-14', 4),
        (14, 987, 987, 987, 987, '2021-10-14', 987),
        (13, 11, 22, 33, 44, '2021-10-14', 55),
        (12, 346, 354, 6346, 2324, '2021-10-11', 34346),
        (11, 25, 634, 364, 235, '2021-10-11', 634),
        (10, 534, 63, 436, 754, '2021-10-11', 364),
        (9, 643, 345, 2356, 34, '2021-10-11', 643),
        (8, 345, 634, 364, 634, '2021-10-11', 643),
        (7, 3425, 435, 53, 35, '2021-10-11', 534),
        (6, 63, 7865, 123, 5632456, '2021-10-11', 230),
        (5, 11, 22, 33, 44, '2021-10-11', 55),
        (4, 1, 2, 3, 4, '2021-10-11', 5),
        (3, 22, 22, 222, 22, '2021-10-10', 222),
        (2, 777, 777, 777, 777, '2021-10-10', 777),
        (1, 22, 33, 44, 55, '2021-10-10', 66)
    ]
    base= [
        {'message': 'zavod_1_92_25', 'id': 1},
        {'message': 'zavod_2_92_25', 'id': 2}
    ]
    if __name__== '__main__':
        app= QApplication (sys.argv)
        ex= MainWindow (okk, base)
        ex.setWindowTitle ('SAMPLE EXAMPLE OF TABLES')
        ex.resize (770, 500)
        ex.show ()
        sys.exit (app.exec_ ())
    

    Here, thanks for that)

    Yan Wolf2021-10-19 03:30:11

    @YanWolf please

    S. Nick2021-10-19 03:30:11
  • Answer # 3

    This is what non-systematic training means. The answer was in Python Beginner Tutorials

    Maybe someone will need it):

    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget, QTableWidgetItem, QVBoxLayout
    from PyQt5.QtGui import QIcon
    from PyQt5.QtCore import pyqtSlot
    okk= [(17, 0, 0, 0, 0, '2021-10-16', 0),
            (16, 22, 22, 33, 44, '2021-10-14', 55),
            (15, 22, 33, 44, 1, '2021-10-14', 4),
            (14, 987, 987, 987, 987, '2021-10-14', 987),
            (13, 11, 22, 33, 44, '2021-10-14', 55),
            (12, 346, 354, 6346, 2324, '2021-10-11', 34346),
            (11, 25, 634, 364, 235, '2021-10-11', 634),
            (10, 534, 63, 436, 754, '2021-10-11', 364),
            (9, 643, 345, 2356, 34, '2021-10-11', 643),
            (8, 345, 634, 364, 634, '2021-10-11', 643),
            (7, 3425, 435, 53, 35, '2021-10-11', 534),
            (6, 63, 7865, 123, 5632456, '2021-10-11', 230),
            (5, 11, 22, 33, 44, '2021-10-11', 55),
            (4, 1, 2, 3, 4, '2021-10-11', 5),
            (3, 22, 22, 222, 22, '2021-10-10', 222),
            (2, 777, 777, 777, 777, '2021-10-10', 777),
            (1, 22, 33, 44, 55, '2021-10-10', 66)]
    base= [{'message': 'zavod_1_92_25', 'id': 1}, {'message': 'zavod_2_92_25', 'id': 2}]
    class App (QWidget):
        def __init __ (self):
            super () .__ init __ ()
            self.title= 'EXAMPLE EXAMPLE OF TABLES'
            self.left= 300
            self.top= 300
            self.width= 1000
            self.height= 800
            self.initUI ()
        def initUI (self):
            self.setWindowTitle (self.title)
            self.setGeometry (self.left, self.top, self.width, self.height)
            self.myAddInTableS ()
            self.layout= QVBoxLayout ()
            self.layout.addWidget (self.tableWidget_1)
            self.layout.addWidget (self.tableWidget_2)
            self.setLayout (self.layout)
            self.show ()
        def myAddInTableS (self):
            self.tableWidget_1= QTableWidget ()
            self.tableWidget_1.setRowCount (4)
            self.tableWidget_1.setColumnCount (7)
            self.tableWidget_2= QTableWidget ()
            self.tableWidget_2.setRowCount (4)
            self.tableWidget_2.setColumnCount (7)
            for item in base:
                message= item ['message']
                id_table= item ['id']
                print ('Sending a command to the server side to trigger the request and send back OKK variable:', message)
    #Vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
                if id_table== 1:
                    certainTable= self.tableWidget_1
                elif id_table== 2:
                    certainTable= self.tableWidget_2
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Set
                certainTable.setRowCount (len (okk)) # <
    ---------------------------
                tablerow= 0
                for row in okk:
                    certainTable.setItem (tablerow, 0, QTableWidgetItem (str (row [0]))) # <
    ----
                    certainTable.setItem (tablerow, 1, QTableWidgetItem (str (row [1]))) # <
    ----
                    certainTable.setItem (tablerow, 2, QTableWidgetItem (str (row [2]))) # <
    ----
                    certainTable.setItem (tablerow, 3, QTableWidgetItem (str (row [3]))) # <
    ----
                    certainTable.setItem (tablerow, 4, QTableWidgetItem (str (row [4]))) # <
    ----
                    certainTable.setItem (tablerow, 5, QTableWidgetItem (row [5])) # <
    ----
                    certainTable.setItem (tablerow, 6, QTableWidgetItem (str (row [6]))) # <
    ----
                    tablerow += 1
    if __name__== '__main__':
        app= QApplication (sys.argv)
        ex= App ()
        sys.exit (app.exec_ ())
    
  • Answer # 4

    This is what non-systematic training means. The answer was in Python Beginner Tutorials

    Maybe someone will need it):

    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget, QTableWidgetItem, QVBoxLayout
    from PyQt5.QtGui import QIcon
    from PyQt5.QtCore import pyqtSlot
    okk= [(17, 0, 0, 0, 0, '2021-10-16', 0),
            (16, 22, 22, 33, 44, '2021-10-14', 55),
            (15, 22, 33, 44, 1, '2021-10-14', 4),
            (14, 987, 987, 987, 987, '2021-10-14', 987),
            (13, 11, 22, 33, 44, '2021-10-14', 55),
            (12, 346, 354, 6346, 2324, '2021-10-11', 34346),
            (11, 25, 634, 364, 235, '2021-10-11', 634),
            (10, 534, 63, 436, 754, '2021-10-11', 364),
            (9, 643, 345, 2356, 34, '2021-10-11', 643),
            (8, 345, 634, 364, 634, '2021-10-11', 643),
            (7, 3425, 435, 53, 35, '2021-10-11', 534),
            (6, 63, 7865, 123, 5632456, '2021-10-11', 230),
            (5, 11, 22, 33, 44, '2021-10-11', 55),
            (4, 1, 2, 3, 4, '2021-10-11', 5),
            (3, 22, 22, 222, 22, '2021-10-10', 222),
            (2, 777, 777, 777, 777, '2021-10-10', 777),
            (1, 22, 33, 44, 55, '2021-10-10', 66)]
    base= [{'message': 'zavod_1_92_25', 'id': 1}, {'message': 'zavod_2_92_25', 'id': 2}]
    class App (QWidget):
        def __init __ (self):
            super () .__ init __ ()
            self.title= 'EXAMPLE EXAMPLE OF TABLES'
            self.left= 300
            self.top= 300
            self.width= 1000
            self.height= 800
            self.initUI ()
        def initUI (self):
            self.setWindowTitle (self.title)
            self.setGeometry (self.left, self.top, self.width, self.height)
            self.myAddInTableS ()
            self.layout= QVBoxLayout ()
            self.layout.addWidget (self.tableWidget_1)
            self.layout.addWidget (self.tableWidget_2)
            self.setLayout (self.layout)
            self.show ()
        def myAddInTableS (self):
            self.tableWidget_1= QTableWidget ()
            self.tableWidget_1.setRowCount (4)
            self.tableWidget_1.setColumnCount (7)
            self.tableWidget_2= QTableWidget ()
            self.tableWidget_2.setRowCount (4)
            self.tableWidget_2.setColumnCount (7)
            for item in base:
                message= item ['message']
                id_table= item ['id']
                print ('Sending a command to the server side to trigger the request and send back OKK variable:', message)
    #Vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
                if id_table== 1:
                    certainTable= self.tableWidget_1
                elif id_table== 2:
                    certainTable= self.tableWidget_2
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Set
                certainTable.setRowCount (len (okk)) # <
    ---------------------------
                tablerow= 0
                for row in okk:
                    certainTable.setItem (tablerow, 0, QTableWidgetItem (str (row [0]))) # <
    ----
                    certainTable.setItem (tablerow, 1, QTableWidgetItem (str (row [1]))) # <
    ----
                    certainTable.setItem (tablerow, 2, QTableWidgetItem (str (row [2]))) # <
    ----
                    certainTable.setItem (tablerow, 3, QTableWidgetItem (str (row [3]))) # <
    ----
                    certainTable.setItem (tablerow, 4, QTableWidgetItem (str (row [4]))) # <
    ----
                    certainTable.setItem (tablerow, 5, QTableWidgetItem (row [5])) # <
    ----
                    certainTable.setItem (tablerow, 6, QTableWidgetItem (str (row [6]))) # <
    ----
                    tablerow += 1
    if __name__== '__main__':
        app= QApplication (sys.argv)
        ex= App ()
        sys.exit (app.exec_ ())