Recovering the mice coordinates relative to a certain window after the button is lit



  • Please provide a way of defining the mouse coordinates relative to a certain window after the button is pressed.

    So we press the button, then we define the coordinates after we leave in a certain window. ЛКМ - the coordinates shall be fixed, removed and the button shall cease.
    She should not determine the coordinates anymore.

    Trying to do something like that:

    import mouse    
    

    ...

    self.pushButton.clicked.connect(self.on_cursorPositionChanged)
    

    def on_cursorPositionChanged(self):
    mouse.on_click(lambda: self.mousePressEvent())

    def mousePressEvent(self, event):
    print('Mouse coords: ( %d : %d )' % (event.x(), event.y()))

    But it works all the time and, yes, the code doesn't work.


    For example:

    введите сюда описание изображения

    In fact, other manipulations will be made to the console, for the sake of visibility, and so upon receipt.



  • Qt Only detects a click inside the view, if you want to detect outside the sights, then you have to use another library, resources D to monitor developments De.g. pynput

    import sys
    from pynput import mouse                                  # pip install pynput
    from PyQt5 import QtCore, QtGui, QtWidgets
    

    class ButtonReleaseManager(QtCore.QObject): # !!!
    released = QtCore.pyqtSignal(int, int)

    def __init__(self, parent=None):
        super().__init__(parent)
        self._listener = mouse.Listener(on_click=self._handle_click)
        self._listener.start()
    
    def _handle_click(self, x, y, button, pressed):
        if not pressed:
            self.released.emit(x, y)
    

    class MainWindow(QtWidgets.QWidget):
    def init(self):
    super().init()
    self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
    self.setGeometry(500, 500, 400, 100)

        self.textEdit = QtWidgets.QTextEdit()
        self.pushButton = QtWidgets.QPushButton('Click me')
        self.pushButton.clicked.connect(self.func_connect)
    
        grid = QtWidgets.QGridLayout(self)
        grid.setSpacing(10)
        grid.addWidget(self.textEdit)
        grid.addWidget(self.pushButton)
    
    def func_connect(self):
        self.manager = ButtonReleaseManager()                       # !!!
        self.manager.released.connect(self.show_position)           # !!!
        self.pushButton.setEnabled(False)
    
    
    @QtCore.pyqtSlot(int, int)
    def show_position(self, x, y):
        screen_coordinate = f"x:{x}, y:{y}"
        self.textEdit.setText(screen_coordinate)
        self.manager.released.disconnect()                          # !!!
    

    if name == "main":
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

    введите сюда описание изображения


    Update

    I'm the one in the view that needs to define the point where it affects the correctness of the display inside the view.

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.Qt import *

    class Label(QtWidgets.QLabel):
    def init(self, parent):
    super().init()
    self.parent = parent

    def mousePressEvent(self, event):
        print(f'def mousePressEvent(self, event): {event.pos()}')
        
    def mouseMoveEvent(self, event):
        if (event.buttons() == Qt.LeftButton): 
            print(f'def mouseMoveEvent(self, event): {event.pos()}') #
    
    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton and self.parent.flag:
            self._pos = event.pos()
            print(f'def mouseReleaseEvent(self, event): {event.pos()} <----') 
            
            self.parent.label_pos.setText(
                f"<b style='color: #fff'>.</b> x:{self._pos.x()}, y:{self._pos.y()}")
            self.parent.label_pos.move(self._pos.x(), self._pos.y())
            self.parent.label_pos.adjustSize()
            self.parent.flag = False
    

    class MainWindow(QtWidgets.QWidget):
    def init(self):
    super().init()

        self.flag = False
        
        self.label = Label(self)
        self.label.setFixedSize(570, 316)
        self.label.setPixmap(QtGui.QPixmap("image1.png"))
        
        self.label_pos = QLabel(self.label)
        
        self.pushButton = QtWidgets.QPushButton('Add', self.label)
        self.pushButton.move(10, 10)
        self.pushButton.clicked.connect(self.func_connect)
    
        grid = QtWidgets.QGridLayout(self)
        grid.addWidget(self.label)
    
    def func_connect(self):
        self.flag = True
        self.pushButton.setEnabled(False)
    

    if name == "main":
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

    введите сюда описание изображения



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2