It is necessary that when you click on the Label event occurred. I found some information here Clickable QLabel and here make QLabel clickable using PyQt5 , but I don’t understand how to use this code.
- sometimes you can use QToolButton instead of QLabel to process a mouse click, for example - jfs
- I still have a QLabel with a picture - I think QToolButton will not work for me. Clicking the button I can handle myself - MyNick
- If you click on the link, you will see that it is the “QLabel with a picture” case that is being emulatedму Your question clearly says that you cannot process it yourself. - jfs
- What link do you mean? QToolButton is the same button - MyNick
- ordinary QLabel does not support mouse clicks. The first comment clearly states why QToolButton. - jfs
|
2 answers
After answering the question about clicks on the label , I saw this and decided to add an example of the simplest implementation of a clickable label:
from PyQt5.Qt import QLabel, pyqtSignal, QApplication, QVBoxLayout, QWidget class ClickedLabel(QLabel): clicked = pyqtSignal() def mouseReleaseEvent(self, e): super().mouseReleaseEvent(e) self.clicked.emit() if __name__ == '__main__': app = QApplication([]) label_1 = ClickedLabel('Label 1') label_1.clicked.connect(lambda: print('label_1')) label_2 = ClickedLabel('Label 2') label_2.clicked.connect(lambda: print('label_2')) layout = QVBoxLayout() layout.addWidget(label_1) layout.addWidget(label_2) mw = QWidget() mw.setLayout(layout) mw.show() app.exec() - Thanks, but this code causes an error at runtime. True, my handler looks a bit different: label_1.clicked.connect (lambda state, x = id: self.method (x)) - MyNick
- @MyNick, and what's the error? - gil9red
- I don't know, I run the script using IDLE and the program fails. How can I see the error? - MyNick
- finally ran your code. Works. Thank. Maybe you can answer my other question: ru.stackoverflow.com/questions/864825/… - MyNick
- @MyNick, accept my answer then :) I looked at the question through the link, unfortunately, it didn’t work with peewee, so I cannot say how to go right away - gil9red
|
Try it:
from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * class LabelClickable(QLabel): clicked = pyqtSignal(str) def __init__(self, parent=None): super(LabelClickable, self).__init__(parent) # Этот обработчик событий для события `event` может быть переопределен # в подклассе для получения событий нажатия мыши для виджета. def mousePressEvent(self, event): self.last = "Click" # Этот обработчик событий для события `event` может быть переопределен в подклассе, # чтобы получать события переноса мыши для виджета. def mouseReleaseEvent(self, event): if self.last == "Click": QTimer.singleShot(QApplication.instance().doubleClickInterval(), self.performSingleClickAction) else: self.clicked.emit(self.last) # Этот обработчик событий для события `event` может быть переопределен в подклассе # для получения событий двойного щелчка мыши для виджета. def mouseDoubleClickEvent(self, event): self.last = "Double Click" def performSingleClickAction(self): if self.last == "Click": self.clicked.emit(self.last) class MainWindow(QDialog): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setWindowTitle("Label Clickable") self.setWindowIcon(QIcon('E:\\_Qt\\img\\qt-logo.png')) self.setWindowFlags(Qt.WindowCloseButtonHint | Qt.MSWindowsFixedSizeDialogHint) self.setFixedSize(400, 511) self.initUI() def initUI(self): self.labelImagen = LabelClickable(self) self.labelImagen.setGeometry(15, 15, 118, 130) self.labelImagen.setToolTip("Image") self.labelImagen.setCursor(Qt.PointingHandCursor) self.labelImagen.setStyleSheet("Qlabel {background-color: white; border: 1px solid " "#01DFD7; border-radius: 5px;}") # Здесь ваша картинка ---> vvvvvvvvvvvvvvvvvvvvvvvvv self.pixmapImagen = QPixmap("E:\\_Qt\\img\\qt-logo.png").scaled(112, 128, Qt.KeepAspectRatio, Qt.SmoothTransformation) self.labelImagen.setPixmap(self.pixmapImagen) self.labelImagen.setAlignment(Qt.AlignCenter) self.labelImagen.clicked.connect(self.Clic) def Clic(self, accion): QMessageBox.information(self, "Тип клика", "Вы сделали {}. ".format(accion)) if __name__ == '__main__': import sys app = QApplication(sys.argv) ex = MainWindow() ex.show() sys.exit(app.exec_()) - Thank you very much. Trying ... - MyNick
|
