pyside实现matplotlib画板嵌入QT界面,通过鼠标与画板中的球交互
时间: 2024-05-21 11:13:20 浏览: 10
可以使用QGraphicsView/QGraphicsScene来实现,将matplotlib画板作为一个QWidget添加到QGraphicsScene中,然后将QGraphicsScene添加到QGraphicsView中。然后通过重载QGraphicsView的mousePressEvent和mouseMoveEvent函数,实现鼠标与画板中的球的交互。具体实现可以参考一些相关的教程和例子。
相关问题
pyside实现matplotlib画板嵌入QT界面,通过鼠标按住拖动画板中的球交互的代码
可以参考下面的代码:
```python
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class MyMplCanvas(FigureCanvas):
def __init__(self):
self.fig = Figure()
self.axes = self.fig.add_subplot(111)
self.axes.set_xlim([0, 10])
self.axes.set_ylim([0, 10])
self.axes.set_xlabel('X')
self.axes.set_ylabel('Y')
self.axes.set_title('My Plot')
self.circle = self.axes.plot([5], [5], 'o', markersize=20, color='r')[0]
FigureCanvas.__init__(self, self.fig)
self.setFixedSize(400, 400)
self.setMouseTracking(True)
self.start_x, self.start_y = None, None
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.start_x, self.start_y = event.x(), event.y()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
dx = event.x() - self.start_x
dy = event.y() - self.start_y
self.circle.set_xdata([self.circle.get_xdata() + dx])
self.circle.set_ydata([self.circle.get_ydata() + dy])
self.draw()
self.start_x, self.start_y = event.x(), event.y()
class MyMainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle('My Window')
self.canvas = MyMplCanvas()
self.setCentralWidget(self.canvas)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyMainWindow()
win.show()
sys.exit(app.exec_())
```
这段代码实现了将 matplotlib 画板嵌入到 PyQt 界面中,并通过鼠标拖动画板中的球实现交互。具体实现过程可以参考代码注释。
pyside嵌入matplot画板并获取它在窗口中的位置
以下是一个示例代码,展示了如何在PySide中嵌入Matplotlib绘图板,并获取它在窗口中的位置:
```python
import sys
import matplotlib.pyplot as plt
from PySide2.QtWidgets import QApplication, QMainWindow, QSizePolicy, QVBoxLayout, QWidget
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建Matplotlib绘图板和画布
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
# 创建一个垂直布局,将画布添加到其中
layout = QVBoxLayout()
layout.addWidget(self.canvas)
# 设置主窗口的中心小部件为该垂直布局
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
# 显示窗口
self.show()
def resizeEvent(self, event):
# 在窗口大小更改时调整画布大小和位置
super().resizeEvent(event)
self.canvas.setGeometry(self.centralWidget().geometry())
# 获取画布在窗口中的位置
pos = self.canvas.mapTo(self.centralWidget(), QPoint(0, 0))
print("Canvas position:", pos)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
```
该示例创建了一个继承自QMainWindow的主窗口,并在其中嵌入了一个Matplotlib绘图板。在`resizeEvent`方法中,它会调整画布的大小和位置,并获取画布在窗口中的位置。最后,通过运行`app.exec_()`启动应用程序的事件循环。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)