0
Follow
0
View

PyQt5in Python - Use a QLabel like a button [duplicate]

dwwsgg 注册会员
2023-01-24 19:56
class ClickableLabel(QtWidgets.QLabel):
    def __init__(self, whenClicked, parent=None):
        QtWidgets.QLabel.__init__(self, parent)
        self._whenClicked = whenClicked

    def mouseReleaseEvent(self, event):
        self._whenClicked(event)

and then:

        my_label = ClickableLabel(self.my_label_clicked)
...
    def my_label_clicked(self, event):
        button = event.button()
        modifiers = event.modifiers()

        if modifiers == Qt.NoModifier and button == Qt.LeftButton:
            logger.debug('my_label_clicked: hooray!')
            return

        LOGGER.debug('my_label_clicked: unhandled %r', event)
tony_nc 注册会员
2023-01-24 19:56

In PyQT5, you can try to add code in setupUi and create clicked method:

def setupUi()
   ... 
   self.label1.linkActivated.connect(self.clicked)
   ...

def clicked(self):
    print(self.label1.text()) #For example, printing the label text...

Hope this helps!

hs0911 未验证用户
2023-01-24 19:56

I do not understand why you pass QMouseEvent to the parent constructor, you must pass the parent attribute as shown below:

class QLabel_alterada(QLabel):
    clicked=pyqtSignal()

    def mousePressEvent(self, ev):
        self.clicked.emit()

To avoid having problems with imports we can directly promote the widget as shown below:

We place a QLabel and right click and choose Promote to ...:

We get the following dialog and place the QLABEL2.h in header file and QLabel_changed in Promoted class Name, then press Add and Promote

Then we generate the .ui file with the help of pyuic. Obtaining the following structure:

├── main.py
├── QLABEL2.py
└── Ui_main.ui

Obtaining the following structure:

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.label.clicked.connect(self.dosomestuff) 

    def dosomestuff(self):
        print("click")


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
cythere 注册会员
2023-01-24 19:56

Since Python can pass function as object, I think make a class inherit QLabel only to make it clickable is overkill. Instead I do like this:

import sys

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFrame, QLabel


def foo(*arg, **kwargs):
   print("Bar")


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    Frame = QFrame()
    label = QLabel(Frame)
    label.mousePressEvent = foo
    label.setText("Label")
    Frame.show()
    sys.exit(app.exec_())

About the Author

Question Info

Publish Time
2023-01-24 19:56
Update Time
2023-01-24 19:56

Related Question

Python的一些简单运用

在python中创建面向对象的多文件项目的requirements.txt

python中如何把data文件转换为三维结构

有没有人能用python画一个波吉啊,如下图

python基础问题

Python -调用变量有3个类

Python函数内部与打开文件:只有第一个列表理解'调用'工作正常?(复制)

如何安装指定的lib-opencv-highgui-dev版本和Python.h问题

如何使用Python“Open Application With:”

Don't understand the meaning of '->' in Python [duplicate]