PyQT5用鼠标移动标签?快速

时间:2019-05-15 21:03:03

标签: python label pyqt5 qlabel

如何更改标签位置?有鼠标事件吗?

了解了如何与鼠标移动同时移动标签后,我便开始搜寻。

附带的是鼠标移动和鼠标标签。

谢谢。

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

x = 0
y = 0


class GUI(QWidget):                                     # Inherit from Qwidget
    def __init__(self):
        super().__init__()                              # Initialize super Class, which creates the Window

        self.initUI()                                   # refer to Window as self

    def initUI(self):
        self.setWindowOpacity(1)
        self.setAttribute(Qt.WA_TranslucentBackground, True)
        self.setStyleSheet("background-color: none;")
        self.setStyleSheet("background-color: rgba(0,0,0);")
        self.cursor_position_coordonates()
        self.showMaximized()

    def cursor_position_coordonates(self):

        self.text = "                    ".format(x, y)
        self.label = QLabel(self.text, self)

        self.label.setStyleSheet("color: rgba(255,255,255);"
                                 "font-size: 62px;"
                                 "font-family: ""Segoe UI"", Light;"
                                 )

        self.label.move(466 , 466)
        self.setMouseTracking(True)


    def mouseMoveEvent(self, e):
        x = e.x()
        y = e.y()

        text = "{0} {1}".format(x, y)
        self.label.setText(text)


if __name__ == "__main__":
    app = QApplication(sys.argv)                        # create Application
    QApplication.processEvents()
    gui = GUI()                                         # create instance of class
    gui.show()                                          #show constructed PyQt window
    sys.exit(app.exec_())                               # execute the application

0 个答案:

没有答案