jxw
jxw

Reputation: 711

Dynamically change GUI element in PyQt5?

Suppose I have a GUI with one button. Upon pressing the button, it should call a definition that in return insert a table in the main GUI window. My script look like below, but upon button press, I don't see any table inserted in the main GUI. Any ideas?

from PyQt5.QtCore import QCoreApplication
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, 
QLineEdit, QLabel, QComboBox, QFileDialog
import mydef

class window(QMainWindow):
    def __init__(self):
    super(window, self).__init__()
    self.setGeometry(50, 50, 500, 300)
    self.setWindowTitle('my test gui')

    #test button
    self.button = QPushButton('Insert table',self)
    self.button.move(5,20)
    self.button.clicked.connect(lambda: mydef.createtable(self))

    self.show()

def run():
    app = QApplication(sys.argv)
    gui = window()
    sys.exit(app.exec_())

run()

mydef is contains the following code

def createtable(self):
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem
self.tablewidget = QTableWidget()
self.tablewidget.resize(500,200)
self.tablewidget.setRowCount(2)
self.tablewidget.setColumnCount(3)
self.tablewidget.setItem(0,0, QTableWidgetItem("item 1"))
self.show()

I don't fully understand why it does not insert the table, and also, it does not seem that I get any error when pressing the button.

Upvotes: 0

Views: 2306

Answers (2)

S. Nick
S. Nick

Reputation: 13651

Try it:

import sys
from PyQt5.QtCore    import QCoreApplication
from PyQt5.QtWidgets import (QApplication, QWidget, QMainWindow, QPushButton, 
                             QLineEdit, QLabel, QComboBox, QFileDialog)
import mydef

class window(QMainWindow):
    def __init__(self):
        super(window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle('my test gui')

        #test button
        self.button = QPushButton('Insert table',self)
        self.button.move(5,20)
        #self.button.clicked.connect(lambda: mydef.createtable(self))
        self.button.clicked.connect(self.create_table)

        self.show()

    def create_table(self):
        self.myTable = mydef.createtable()
        self.myTable.show()


def run():
    app = QApplication(sys.argv)
    gui = window()
    sys.exit(app.exec_())

run()

mydef.py

from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem

def createtable():
    tablewidget = QTableWidget()
    tablewidget.resize(500,200)
    tablewidget.setRowCount(2)
    tablewidget.setColumnCount(3)
    tablewidget.setItem(0,0, QTableWidgetItem("item 1"))

    return tablewidget

enter image description here

main_2.py

import sys
from PyQt5.QtCore    import QCoreApplication
from PyQt5.QtWidgets import (QApplication, QWidget, QMainWindow, QPushButton, QVBoxLayout,
                             QLineEdit, QLabel, QComboBox, QFileDialog)
import mydef

class window(QMainWindow):
    def __init__(self):
        super(window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle('my test gui')

        self.central_widget = QWidget()               
        self.setCentralWidget(self.central_widget)    
        self.lay = QVBoxLayout(self.central_widget)

        #test button
        self.button = QPushButton('Insert table', self)
        self.button.move(5,20)
        #self.button.clicked.connect(lambda: mydef.createtable(self))
        self.button.clicked.connect(self.create_table)

        self.lay.addWidget(self.button)
        self.lay.addStretch(1)


        self.show()

    def create_table(self):
        self.myTable = mydef.createtable()

        self.lay.addWidget(self.myTable)
        self.lay.addStretch(1)


def run():
    app = QApplication(sys.argv)
    gui = window()
    sys.exit(app.exec_())

run()

enter image description here

Upvotes: 2

MalloyDelacroix
MalloyDelacroix

Reputation: 2293

You cannot add the table to the current GUI by calling the show() method. If you use a layout for the main window you can simply add the table to the layout.

Here is an example:

class GUITest(QtWidgets.QWidget):

    def __init__(self):
        QtWidgets.QWidget.__init__(self)

        self.layout = QtWidgets.QGridLayout()

        self.button = QtWidgets.QPushButton('Insert Table')
        self.button.clicked.connect(self.create_table)

        self.layout.addWidget(self.button)

        self.setLayout(self.layout)

    def create_table(self):
        self.tablewidget = QtWidgets.QTableWidget()
        self.tablewidget.resize(500, 200)
        self.tablewidget.setRowCount(2)
        self.tablewidget.setColumnCount(3)
        self.tablewidget.setItem(0, 0, QtWidgets.QTableWidgetItem("item 1"))

        self.layout.addWidget(self.tablewidget)  # add the table to the layout

If the table has to be added from another class the way that you show, you can supply the layout to the create_table method and use basically the same method as above.

def create_table(self, layout):
    # create table widget
    layout.addWidget(self.tablewidget)

Upvotes: 1

Related Questions