在PyQt5中动态更改GUI元素?

时间:2018-06-11 16:58:19

标签: python pyqt5

假设我有一个带有一个按钮的GUI。按下按钮后,它应调用一个定义,作为回报,在主GUI窗口中插入一个表。我的脚本如下图所示,但按下按钮后,我看不到主GUI中插入的任何表格。有什么想法吗?

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包含以下代码

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()

我不完全理解为什么它不插入表格,而且,按下按钮时似乎没有任何错误。

2 个答案:

答案 0 :(得分:1)

您无法通过调用show()方法将表添加到当前GUI。如果您使用主窗口的布局,只需将表格添加到布局中即可。

以下是一个例子:

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

如果必须按照您显示的方式从其他类添加表,您可以为create_table方法提供布局,并使用与上述基本相同的方法。

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

答案 1 :(得分:1)

试一试:

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