如何将附加的简单表格放置在 PyQtGraph 中
Posted
技术标签:
【中文标题】如何将附加的简单表格放置在 PyQtGraph 中【英文标题】:How to place the simple table attached in a PyQtGraph 【发布时间】:2022-01-12 22:05:29 【问题描述】:下面的代码使用 QTableWidget 创建了一个表格,然后它创建了两个 PyQtGraphs,我需要将表格放在第二个图表中,以便表格的边框与图表的边框重合(行是固定的,而列不是,但为了简单起见,让我们使用具有固定列的表)。表中的数字并不重要(它们将基于对第一张图表数据的计算)。我怎样才能达到这个结果?
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
from PyQt6.QtWidgets import QTableWidget,QTableWidgetItem,QVBoxLayout
import sys
app = QtGui.QApplication([])
view = pg.GraphicsView()
l = pg.GraphicsLayout()
view.setCentralItem(l)
view.show()
view.resize(800,600)
p0 = l.addPlot(0, 0)
p0.showGrid(x = True, y = True, alpha = 1.0)
#have no x-axis tickmark below the upper plot (coordinate 0,0)
#without these lines, there will be separate coordinate systems with a gap inbetween
ax0 = p0.getAxis('bottom') #get handle to x-axis 0
ax0.setStyle(showValues=False) #this will remove the tick labels and reduces gap b/w plots almost to zero
#there will be a double line separating the plot rows
p1 = l.addPlot(1, 0)
p1.showGrid(x = True, y = True, alpha = 1.0)
p1.setXLink(p0)
p1.setMouseEnabled(y=False, x=False)
l.layout.setSpacing(0.)
l.setContentsMargins(0., 0., 0., 0.)
#Table
data = 'col1':['1','2','3','4'],
'col2':['1','2','1','3'],
'col3':['1','1','2','1']
class TableView(QTableWidget):
def __init__(self, data, *args):
QTableWidget.__init__(self, *args)
self.data = data
self.setData()
self.resizeColumnsToContents()
self.resizeRowsToContents()
def setData(self):
horHeaders = []
for n, key in enumerate(sorted(self.data.keys())):
horHeaders.append(key)
for m, item in enumerate(self.data[key]):
newitem = QTableWidgetItem(item)
self.setItem(m, n, newitem)
self.setHorizontalHeaderLabels(horHeaders)
table = TableView(data, 4, 3)
table.show()
if __name__ == '__main__':
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec()
【问题讨论】:
【参考方案1】:我不是 100% 确定我理解你试图用你的代码得到什么,但是,如果你想在你的 Plot 下方放置一个 TableWidget,你可以只使用 QVBoxLayout 然后添加你的绘图和表格。您必须将数据分别传递给绘图和表格。 Pyqtgraph 还有一个表格小部件,您可以将字典和列表传递给它,这样您就不必从头开始创建 qt5 表格小部件。
以下是我将如何使用主窗口小部件类编写程序:
import sys
from PyQt5.QtWidgets import (QVBoxLayout, QMainWindow, QWidget, QApplication)
import pyqtgraph as pg
class plotAndTableWidget(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
self.create_data()
def initUI(self):
self.central_widget = QWidget()
self.vertical_layout = QVBoxLayout()
self.setCentralWidget(self.central_widget)
self.central_widget.setLayout(self.vertical_layout)
self.plot = pg.PlotWidget()
self.table = pg.TableWidget()
#The 1 after the plot and table essentially tells Qt to expand
#those widgets evenly so they should take up a similar amount of space.
self.vertical_layout.addWidget(self.plot, 1)
self.vertical_layout.addWidget(self.table, 1)
def create_data(self):
data = 'col1':[1, 2, 3, 4],
'col2':[1, 2, 1, 3],
'col3':[1, 1 ,2, 1]
self.table.setData(data)
for i in data.keys():
self.plot.plot(y=data[i])
def run_program():
app = QApplication(sys.argv)
window = plotAndTableWidget()
window.show()
app.exec()
#If you want to close your python interpreter you will need to add
#sys.exit(app.exec_()) instead of app.exec().
if __name__ == '__main__':
run_program()
【讨论】:
以上是关于如何将附加的简单表格放置在 PyQtGraph 中的主要内容,如果未能解决你的问题,请参考以下文章