2019-05-02 19:11:22 +08:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# encoding: utf-8
|
2021-07-13 14:52:26 +08:00
|
|
|
|
"""
|
2019-05-21 17:54:22 +08:00
|
|
|
|
Created on 2019年5月2日
|
2019-05-02 19:11:22 +08:00
|
|
|
|
@author: weike32
|
2021-07-13 14:52:26 +08:00
|
|
|
|
@site: https://pyqt.site ,https://github.com/weike32
|
2019-05-02 19:11:22 +08:00
|
|
|
|
@email: 394967319@qq.com
|
|
|
|
|
@file: CopyContent
|
|
|
|
|
@description: 查阅了很多博客,如果有异,可以联系作者邮箱。本Demo仅作学习参考用,保有后续相关权益。
|
2021-07-13 14:52:26 +08:00
|
|
|
|
"""
|
2019-05-02 19:11:22 +08:00
|
|
|
|
import sys
|
2021-07-13 14:52:26 +08:00
|
|
|
|
|
2019-05-02 19:11:22 +08:00
|
|
|
|
import numpy as np
|
|
|
|
|
import pyqtgraph as pg
|
2021-07-13 14:52:26 +08:00
|
|
|
|
from PyQt5 import QtCore
|
|
|
|
|
from PyQt5.QtWidgets import QApplication, QMainWindow
|
|
|
|
|
|
2019-05-02 19:11:22 +08:00
|
|
|
|
|
|
|
|
|
class Ui_Form(object):
|
|
|
|
|
def setupUi(self, Form):
|
|
|
|
|
Form.setObjectName("Form")
|
|
|
|
|
Form.resize(726, 595)
|
|
|
|
|
self.graphicsView = pg.PlotWidget(Form)
|
|
|
|
|
self.graphicsView.setGeometry(QtCore.QRect(75, 131, 621, 441))
|
|
|
|
|
self.graphicsView.setObjectName("graphicsView")
|
|
|
|
|
|
2021-07-13 14:52:26 +08:00
|
|
|
|
|
2019-05-02 19:11:22 +08:00
|
|
|
|
class MyWindow(QMainWindow, Ui_Form):
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
super(MyWindow, self).__init__(parent)
|
|
|
|
|
self.setupUi(self)
|
|
|
|
|
x = np.linspace(-100, 100, 1000)
|
|
|
|
|
data = np.sin(x) / x
|
|
|
|
|
self.graphicsView.plot(data, pen=(255, 255, 255, 200))
|
|
|
|
|
self.label = pg.TextItem(text="横坐标:{}".format(0))
|
|
|
|
|
self.graphicsView.addItem(self.label)
|
|
|
|
|
self.setMouseTracking(True)
|
|
|
|
|
self.graphicsView.scene().sigMouseMoved.connect(self.onMouseMoved)
|
2021-07-13 14:52:26 +08:00
|
|
|
|
|
2019-05-02 19:11:22 +08:00
|
|
|
|
def onMouseMoved(self, evt):
|
|
|
|
|
if self.graphicsView.plotItem.vb.mapSceneToView(evt):
|
2021-07-13 14:52:26 +08:00
|
|
|
|
point = self.graphicsView.plotItem.vb.mapSceneToView(evt)
|
2019-05-02 19:11:22 +08:00
|
|
|
|
self.label.setHtml("<p style='color:white'>横坐标:{0}</p>".format(point.x()))
|
2021-07-13 14:52:26 +08:00
|
|
|
|
|
|
|
|
|
|
2019-05-02 19:11:22 +08:00
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
|
myWin = MyWindow()
|
|
|
|
|
myWin.show()
|
2021-07-13 14:52:26 +08:00
|
|
|
|
sys.exit(app.exec_())
|