0

How to add my Labe by mouse left clicking, for example, in the following code, how to call a Todo function by clicking in any part of my app window and add lable here?

import sys
from PyQt5.QtWidgets import(QApplication, QLabel, QWidget)
import sys


class MouseTracker(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.setMouseTracking(True)

    def initUI(self):
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Mouse Tracker')
        self.show()

    def mousePressEvent(self, event):
        print("x , y")


    def todo(self):
    self.label = QLabel('This is label', self)

if __name__ == '__main__':
   app = QApplication(sys.argv)
ex = MouseTracker()
sys.exit(app.exec_())
  • Does this answer your question? [PyQt5: Check if mouse is held down in enter-event](https://stackoverflow.com/questions/46147290/pyqt5-check-if-mouse-is-held-down-in-enter-event) – anirudh Nov 27 '21 at 10:59
  • no my dear friend, i just want to by clicking mouse left button , call my todo fuction – Ailaqi Barca Nov 27 '21 at 11:51
  • Does anyone have an answer to this question? – Ailaqi Barca Nov 27 '21 at 13:53
  • @AilaqiBarca if anybody has an answer, they will post it; asking it is pointless, and comments should only be used to add useful content to the post. That said, you already overridden `mousePressEvent()`, call `todo()` from there, and add `self.label.show()`, as child widgets created *after* the parent has been made visible are hidden by default. Also note that you should use [layout managers](https://doc.qt.io/qt-5/layout.html), and you should not continuously overwrite an instance attribute when creating objects dynamically. – musicamante Nov 27 '21 at 14:14
  • tanks, How to dynamically add the label widget in the same part with each click in the desired section? – Ailaqi Barca Nov 27 '21 at 14:39
  • My goal is to add a Todo function to a custom place by clicking on the mouse. – Ailaqi Barca Nov 27 '21 at 14:41
  • 1
    Add a `pos` argument to the `todo` method, and also add `label.move(pos)` before calling `label.show()`. Then in `mousePressEvent`, do `self.todo(event.pos())`. – ekhumoro Nov 27 '21 at 14:43
  • I'm very sorry But is it possible to add this option in my code? I have not received this answer for a week – Ailaqi Barca Nov 27 '21 at 15:07

0 Answers0