如何在PYQT中创建标签按钮?

时间:2016-05-24 11:53:12

标签: python pyqt

我的应用需要你的帮助。我未修饰python gui,现在我需要创建clicable标签。

run.py

from PyQt4 import  QtCore, QtGui
import sys

import untitled

class ExampleApp(QtGui.QMainWindow, untitled.Ui_MainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)



def main():
    app = QtGui.QApplication(sys.argv)
    form = ExampleApp()
    form.show()
    app.exec_()


if __name__ == '__main__':
    main()                              


def closeapp():
    sys.exit(0);

untitled.py:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

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(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(40, 110, 85, 30))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(220, 100, 58, 14))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(220, 140, 58, 14))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        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.pushButton.setText(_translate("MainWindow", "open", None))
        self.label.setText(_translate("MainWindow", "close", None))
        self.label_2.setText(_translate("MainWindow", "view", None))

现在我需要为label和label_2添加操作。如何使标签执行closeapp()?

2 个答案:

答案 0 :(得分:4)

向Ui_MainWindow类添加一个函数(untitled.py)

def click(self,eve):
    print "clicked"

然后将属性添加到标签对象

self.label.mousePressEvent = self.click

另一个选项就是修改run.py文件,如下面的

class ExampleApp(QtGui.QMainWindow, untitled.Ui_MainWindow):
    def click(self,eve):
        print "clicked"
    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.label.mousePressEvent = self.click
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

答案 1 :(得分:2)

QLabel继承QWidgetQWidget`有一个事件处理程序mousePressEvent

提到的E-ebola病毒

添加

self.label.mousePressEvent = self.click

并定义

def click(self,event):
    #do something

子类QLabel

class customLabel(QLabel):
    def __init__(self):
       QLabel.__init__(self)

    def self.mousePressEvent(self,event):
        self.emit(SIGNAL("closeapp"))

在按下标签时发出SIGNAL,我会将其实现为

self.label = customLabel()
self.connect(self.label,SIGNAL('closeapp'),self.close)