Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

python - How can I use or return the coordinates of pyqtgraph, after clicking, outside the mousePressed function?

I want to access the coordinates outside the mousePress function. What I want is:

call self.curve.scene().sigMouseClicked.connect(self.mousePress) when PushButton is pressed. But I am unable to return the values from mousePress function.

import sys
import platform
from PySide2.QtWidgets import QApplication, QWidget, QPushButton
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCore import (QCoreApplication, QPropertyAnimation, QDate, QDateTime, QMetaObject,
    QObject, QPoint, QRect, QSize, QTime, QUrl, Qt, QEvent)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
    QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter,
    QPixmap, QRadialGradient)
from PySide2.QtWidgets import *
from PySide2.QtGui import QPainter, QBrush, QPen, QPainterPath
import numpy as np
import math
from PySide2 import QtWidgets
from pyqtgraph.Qt import QtGui, QtCore
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg

import os


#import serial
import time

#ser = serial.Serial('COM5', 9600)
class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.graphWidget = pg.PlotWidget()
        self.setCentralWidget(self.graphWidget)
        

        self.button = QPushButton('Run', self)
        self.button.setToolTip('Click to run.')
        self.button.move(100,70)

        hour = []
        temperature = []

        

        x_for_line = [0, 0]
        y_for_line = [0, 10]
        x=0
        
        
        r = 10
        
        theta = 0
        while (theta <= 180):
            hour.append(  r* math.sin(math.radians(theta)))
            temperature.append(r * math.sin(math.radians(90-theta)))
            theta = theta + 0.1
        
        #Add grid
        self.graphWidget.showGrid(x=True, y=True)
        self.graphWidget.setBackground('w')

        pen = pg.mkPen(color=(94, 186, 95), width=8)

        pen2 = pg.mkPen(color=(13, 31, 45), width=2)
        

        self.graphWidget.plot(x_for_line, y_for_line, pen=pen2)

        

        self.graphWidget.plot(temperature, hour, pen=pen)
        
    
        self.curve = self.graphWidget.plot(x_for_line,y_for_line , pen=pen)

        self.curve.scene().sigMouseMoved.connect(self.onMouseMoved)

        #if(btn_pressed):
        self.curve.scene().sigMouseClicked.connect(self.mousePress)

        
        # if(self.button.clicked.connect(self.on_click())):
        #     self.curve.scene().sigMouseClicked.connect(self.mousePress)
        #     k=0

    # def on_click(self):
    #     return True

    def onMouseMoved(self, point):
        p = self.graphWidget.plotItem.vb.mapSceneToView(point)
        self.statusBar().showMessage("{}-{}".format(p.x(), p.y()))
        
    def mousePress(self, event):
        global p_list
        p_list = []
        items = self.graphWidget.scene().items(event._scenePos)
        mousePoint = self.graphWidget.plotItem.vb.mapSceneToView(event._scenePos)
        self.graphWidget.clear()
        # print((mousePoint.x()),(mousePoint.y()))
        p_list = [mousePoint.x(), mousePoint.y()]
        return p_list
        

def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
question from:https://stackoverflow.com/questions/65867595/how-can-i-use-or-return-the-coordinates-of-pyqtgraph-after-clicking-outside-th

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...