用pyqt实现:一个界面输出x数据,和y数据,另一个界面实时接收,并将这些数据作为横坐标和纵坐标绘制折线图
时间: 2023-06-26 07:10:23 浏览: 64
可以使用PyQt中的信号与槽机制来实现两个界面之间的数据传输,使用matplotlib库来绘制折线图。以下是一个简单的代码示例:
```python
import sys
import random
import matplotlib.pyplot as plt
from PyQt5.QtCore import pyqtSignal, Qt, QObject
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel
class Data(QObject):
dataChanged = pyqtSignal(float, float)
def __init__(self):
super().__init__()
def update_data(self):
while True:
x = random.uniform(0, 10)
y = random.uniform(0, 10)
self.dataChanged.emit(x, y)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Main Window')
self.data = Data()
self.data.dataChanged.connect(self.update_plot)
self.data.update_data()
self.label_x = QLabel('X:')
self.label_y = QLabel('Y:')
self.text_x = QLabel()
self.text_y = QLabel()
layout = QVBoxLayout()
layout.addWidget(self.label_x)
layout.addWidget(self.text_x)
layout.addWidget(self.label_y)
layout.addWidget(self.text_y)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
def update_plot(self, x, y):
self.text_x.setText(str(x))
self.text_y.setText(str(y))
self.plot_window.update_plot(x, y)
class PlotWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Plot Window')
self.figure, self.ax = plt.subplots()
self.line, = self.ax.plot([], [])
layout = QHBoxLayout()
layout.addWidget(self.figure.canvas)
self.setLayout(layout)
def update_plot(self, x, y):
self.line.set_xdata(list(self.line.get_xdata()) + [x])
self.line.set_ydata(list(self.line.get_ydata()) + [y])
self.ax.relim()
self.ax.autoscale_view(True, True, True)
self.figure.canvas.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
plot_window = PlotWindow()
main_window.plot_window = plot_window
plot_window.show()
sys.exit(app.exec_())
```
在这个例子中,`Data`类作为一个数据源,不断地生成随机的x和y数据,并通过`dataChanged`信号发送给`MainWindow`类。`MainWindow`类接收到数据后,更新界面上的文本框,并将数据传递给`PlotWindow`类,用于绘制折线图。`PlotWindow`类使用matplotlib库绘制折线图,并在接收到新数据时更新图形。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)