[英]How do I add a QPushButton to PyQtGraph using addItem?
I am trying to add a button at the bottom of two plots that will display data to read in from a file.我正在尝试在两个图的底部添加一个按钮,该按钮将显示要从文件中读取的数据。 Underneath these two plots will be a button to control an action.
在这两个图下方将是一个控制动作的按钮。 I have attempted to add a widget, layout, graphicsItem from the pyqt library.
我试图从 pyqt 库中添加一个小部件、布局、图形项。 I can easily add a label to the layout but when adding a button I get the following error
我可以轻松地向布局添加标签,但是在添加按钮时出现以下错误
addItem(self, QGraphicsLayoutItem, int, int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'QPushButton'
The code being tested:正在测试的代码:
import pyqtgraph as pg
win = pg.GraphicsWindow()
win.setWindowTitle('Test App')
label = pg.LabelItem(justify='right')
win.addItem(label)
button = QtGui.QPushButton()
p1 = win.addPlot(row=0, col=0)
p2 = win.addPlot(row=1, col=0)
p3 = win.addLayout(row=2, col=0)
p3.addItem(button,row=1,col=1)
The pyqtgraph documentation on addItem states that it "adds a graphics item to the view box." addItem上的 pyqtgraph 文档指出它“将图形项添加到视图框”。
The thing is, a QtPushButton
is not a graphic item, it's a widget.问题是,
QtPushButton
不是图形项目,它是一个小部件。 Therefore the error: addItem
is expecting a QGraphicsLayoutItem
(or something that inherits that class), and you're passing a QWidget
因此错误:
addItem
需要一个QGraphicsLayoutItem
(或继承该类的东西),而您正在传递一个QWidget
To add a widget into a GraphicsWindow
, you could wrap it with QGraphicsProxyWidget
要将小部件添加到
GraphicsWindow
,您可以使用QGraphicsProxyWidget
包装它
proxy = QtGui.QGraphicsProxyWidget()
button = QtGui.QPushButton('button')
proxy.setWidget(button)
p3 = win.addLayout(row=2, col=0)
p3.addItem(proxy,row=1,col=1)
But depending on what you need to do, you might want to implement a PyQt GUI, with the GraphicsWindow
being one element of this GUI.但是根据您需要做什么,您可能想要实现一个 PyQt GUI,
GraphicsWindow
是这个 GUI 的一个元素。 This question could help you: How to update a realtime plot and use buttons to interact in pyqtgraph?这个问题可以帮助您: 如何更新实时绘图并使用按钮在 pyqtgraph 中进行交互?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.