Qt GUI可点击Qframe或QWidget或Groupbox

时间:2016-05-30 09:05:53

标签: python qt python-3.x

我的代码有问题:/ 我的程序需要单击QFrame,QWidget或QGroupBox。 目前我使用mouseReleaseEvent但它只在我的函数没有设置值时才有效。

def testc(self,e):
 print(e)

#when I use this code:
self.frame.mousePressEvent = self.testc;
#it's okey
#but when I'm using
self.frame.mousePressEvent = self.testc("ssssss");
# after starting the function automatically performs a click not working

enter image description here 如何制作点击小部件,框架或分组框?

我的qt代码

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

# Form implementation generated from reading ui file 'ar.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_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(571, 149)
        self.frame = QtGui.QFrame(Form)
        self.frame.setGeometry(QtCore.QRect(30, 30, 120, 80))
        self.frame.setStyleSheet(_fromUtf8("background-color: rgb(88, 169, 255);"))
        self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtGui.QFrame.Raised)
        self.frame.setObjectName(_fromUtf8("frame"))
        self.label = QtGui.QLabel(self.frame)
        self.label.setGeometry(QtCore.QRect(17, 23, 91, 31))
        self.label.setObjectName(_fromUtf8("label"))
        self.widget = QtGui.QWidget(Form)
        self.widget.setGeometry(QtCore.QRect(210, 30, 120, 80))
        self.widget.setStyleSheet(_fromUtf8("background-color: rgb(255, 158, 160);"))
        self.widget.setObjectName(_fromUtf8("widget"))
        self.label_2 = QtGui.QLabel(self.widget)
        self.label_2.setGeometry(QtCore.QRect(30, 40, 81, 31))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.groupBox = QtGui.QGroupBox(Form)
        self.groupBox.setGeometry(QtCore.QRect(380, 30, 120, 80))
        self.groupBox.setStyleSheet(_fromUtf8("background-color: rgb(56, 30, 255);"))
        self.groupBox.setObjectName(_fromUtf8("groupBox"))
        self.label_3 = QtGui.QLabel(self.groupBox)
        self.label_3.setGeometry(QtCore.QRect(7, 34, 101, 21))
        self.label_3.setObjectName(_fromUtf8("label_3"))

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

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.label.setText(_translate("Form", "clicable frame", None))
        self.label_2.setText(_translate("Form", "click widget", None))
        self.groupBox.setTitle(_translate("Form", "GroupBox", None))
        self.label_3.setText(_translate("Form", "click groupbox", None))

关键是我需要QFrame中的一组对象。单击QFrame后,我希望我的程序在单击后编写。

1 个答案:

答案 0 :(得分:1)

创建一个不带参数的函数

您必须为mousePressEvent分配一个函数,而self.testc("ssssss")(可能)不会返回函数。

您可以做的是创建另一个功能

def f(self):
    return self.testc("ssssss")

并指定

self.frame.mousePressEvent = self.f

对于这样的单行,通常首选创建 lambda

self.frame.mousePressEvent = lambda: self.tests("ssssss")