PyQt中的可点击QLabel?

时间:2018-04-05 10:46:18

标签: python qt pyqt pyqt4 qt-designer

我正在使用pyqt4创建侧边菜单。

我将QLabel(burgermenu)变成了汉堡菜单的图片,我无法点击它。

我的意思是,当我点击burgermenu图像时,内框(内框)完全隐藏,当我再次点击burgermenu时;内框出现及其内容。

我怎样才能做到这一点?

这是我的代码:

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s
try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(479, 381)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.outerframe = QtGui.QFrame(self.centralwidget)
        self.outerframe.setGeometry(QtCore.QRect(190, 80, 151, 211))
        self.outerframe.setFrameShape(QtGui.QFrame.StyledPanel)
        self.outerframe.setFrameShadow(QtGui.QFrame.Raised)
        self.outerframe.setObjectName(_fromUtf8("outerframe"))
        self.burgermenu = QtGui.QLabel(self.outerframe)
        self.burgermenu.setGeometry(QtCore.QRect(70, 10, 71, 20))
        self.burgermenu.setObjectName(_fromUtf8("burgermenu"))
        self.innerframe = QtGui.QFrame(self.outerframe)
        self.innerframe.setGeometry(QtCore.QRect(10, 60, 131, 141))
        self.innerframe.setFrameShape(QtGui.QFrame.StyledPanel)
        self.innerframe.setFrameShadow(QtGui.QFrame.Raised)
        self.innerframe.setObjectName(_fromUtf8("innerframe"))
        self.widget = QtGui.QWidget(self.innerframe)
        self.widget.setGeometry(QtCore.QRect(20, 30, 91, 99))
        self.widget.setObjectName(_fromUtf8("widget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.widget)
        self.verticalLayout.setMargin(0)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.label_2 = QtGui.QLabel(self.widget)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.verticalLayout.addWidget(self.label_2)
        self.dateEdit = QtGui.QDateEdit(self.widget)
        self.dateEdit.setObjectName(_fromUtf8("dateEdit"))
        self.verticalLayout.addWidget(self.dateEdit)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 479, 22))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.burgermenu.setText(_translate("MainWindow", "burgermenu", None))
        self.burgermenu.setPixmap(QPixmap("/Users/Desktop/menu.png"))
        self.label_2.setText(_translate("MainWindow", "Date:", None))

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:0)

您可以覆盖$d = new DateTime('04-06-2018'); $todayDate = new DateTime(); $interval = $d->diff($todayDate); $days = $interval->format('%a'); if($days >= 2) {echo 'true'; } else {echo 'false';} 的{​​{3}},如:

QLabel

您只需在初始化或启动期间调用一次:

def setupClickToggle(self):
    def mousePressEvent(*args, **kwargs):
        self.innerframe.setVisible(not self.innerframe.isVisible())
    self.burgermenu.mousePressEvent = mousePressEvent

答案 1 :(得分:0)

您可以在此处使用方法名称self.burgermenu.mousePressEvent = self.dosomething

self.innerframe.hide()

在dosomething方法之后,您可以调用内部框架名为hide或show的方法。

self.innerframe.show()

event

并且不要忘记在dosomething

中添加Shiny作为参数