使用Python和PyQt5,我按照GUI编程 现在我想自定义程序,以便我能够通过单击右边的表格边框(浅蓝色线条)并移动鼠标来更改左表格宽度(请参见图中的箭头)。在改变桌子宽度的同时,右侧也应该改变。 我希望我的描述清楚。
GUI输出:
GUI布局的部分代码:
# table for writeOutput
self.tableWidget = QTableWidget(0, 2)
self.tableWidget.verticalHeader().setVisible(False);
self.tableWidget.setMinimumHeight(170)
self.tableWidget.setMinimumWidth(600)
# generate chart
self.main_widget = QWidget(self)
self.chart = QtBarChart(self.main_widget)
# Layout for input elements
inputLayout = QHBoxLayout()
inputLayout.addWidget(self.typeCBox)
inputLayout.addWidget(self.yearSpinBox)
inputLayout.addWidget(self.button)
inputLayout.addStretch(1)
# right inner layout for output elements e.g chart
innerOutLayout = QVBoxLayout()
innerOutLayout.addWidget(self.chart)
innerOutLayout.addWidget(self.outEdit)
# outer layout for output elements e.g. table
outLayout = QHBoxLayout()
outLayout.addWidget(self.tableWidget)
outLayout.addLayout(innerOutLayout)
# top layout who groups all elements together
topLevelLayout = QVBoxLayout()
topLevelLayout.addLayout(inputLayout)
topLevelLayout.addLayout(outLayout)
实现目标需要进行哪些功能或代码更改?
- > QSplitter是一个很好的提示
当我尝试实现QSplitter并更改我的程序时,我得到一个错误。
# Layout for input elements
inputLayout = QHBoxLayout()
inputLayout.addWidget(self.typeCBox)
inputLayout.addWidget(self.yearSpinBox)
inputLayout.addWidget(self.button)
inputLayout.addStretch(1)
# right inner layout for output elements e.g chart
innerOutLayout = QVBoxLayout()
innerOutLayout.addWidget(self.chart)
innerOutLayout.addWidget(self.outEdit)
# create Splitter between Table and innerOutLayout() --> (Chart and outEdit)
tableSplitter = QSplitter(Qt.Horizontal)
tableSplitter.addWidget(self.tableWidget)
tableSplitter.addLayout(innerOutWidget)
# outer layout for output elements e.g. table
outLayout = QHBoxLayout()
outLayout.addWidget(self.tableWidget)
outLayout.addWidget(tableSplitter)
# top layout who groups all elements together
topLevelLayout = QVBoxLayout()
topLevelLayout.addLayout(inputLayout)
topLevelLayout.addLayout(outLayout)
当我运行该程序时,我收到以下错误:
tableSplitter.addLayout(innerOutWidget)
AttributeError: 'QSplitter' object has no attribute 'addLayout'
如何解决此错误以及如何更改代码才能正常工作。
答案 0 :(得分:1)
解决方案是使用QSplitter
,这些与QLayout
类似,目标是控制小部件的大小。
在以下示例中,示例显示了它的用法。
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent=parent)
self.verticalLayout = QtWidgets.QVBoxLayout(self)
self.splitter = QtWidgets.QSplitter(self)
self.splitter.setOrientation(QtCore.Qt.Horizontal)
self.createTable(self.splitter, 2, 2)
self.splitter1 = QtWidgets.QSplitter(self.splitter)
self.splitter1.setOrientation(QtCore.Qt.Vertical)
self.createTable(self.splitter1, 3, 3)
self.createTable(self.splitter1, 4, 4)
self.verticalLayout.addWidget(self.splitter)
def createTable(self, parent, nrows, ncols):
tableWidget = QtWidgets.QTableWidget(parent)
tableWidget.setRowCount(nrows)
tableWidget.setColumnCount(ncols)
for i in range(tableWidget.rowCount()):
for j in range(tableWidget.columnCount()):
item = QtWidgets.QTableWidgetItem(f"{i}{j}")
tableWidget.setItem(i, j, item)
return tableWidget
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())