自定义按钮 PyQT4 [英] Custom button PyQT4

查看:55
本文介绍了自定义按钮 PyQT4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法显示使用 Python 2.7 从 PyQt4 中的类创建的按钮.我的项目有 80 个按钮,它们都需要拖放功能.我创建了一个允许这样做的按钮类,但我不确定如何在 Ui_Form 中显示它.

I'm having trouble displaying a button that I create from a class in PyQt4, using Python 2.7. My project has 80 buttons that all require a drag and drop function. I've created a button class that allows this, but I'm unsure as to how to display it within Ui_Form.

我不认为我可以使用布局管理器,因为我需要背景图像,所以我假设我需要使用 Ui_Form<中的 Form 参数从 Button 类创建按钮/code> 的 setupUi 功能,但我确定如何.

I don't think I can use a layout manager as I want a background image so I assume I need to create the button from the Button class using the Form argument from Ui_Form's setupUi function, but I'm insure as to how.

下面是一个只有两个按钮(和一个屏幕截图)的脚本示例——一个是从我的班级调用的,另一个是 QT Designer 留下的.按钮 btn_a1 应该在 btn_a2 的左侧可见,但它不是(因为不知道如何正确调用它).为了便于使用,我删除了按钮标签和功能的代码.

Below is an example of the script with only two buttons (and a screenshot)- one called from my class and one just as QT Designer left it. The button btn_a1 should be visible to the left of btn_a2, but it isn't (due to not knowing how to properly call it). I've removed the code for the labels and functionality of the buttons to help with ease of use.

import sys
from PyQt4 import QtCore, QtGui

class Ui_Form(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(591, 591)
        self.Background = QtGui.QLabel(Form)
        self.Background.setGeometry(QtCore.QRect(0, 0, 631, 591))
        self.Background.setPixmap(QtGui.QPixmap("Python/LP_Proj/LP_Background.png"))
        self.Background.setObjectName("Background")

        self.btn_a1 = Button(Form)

        self.btn_a2 = QtGui.QPushButton(Form)
        self.btn_a2.setGeometry(QtCore.QRect(90, 40, 41, 41))
        self.btn_a2.setObjectName("btn_a2")

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

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Launchpad Control", None, QtGui.QApplication.UnicodeUTF8))

class Button(QtGui.QPushButton):
    def __init__(self, Form):
        super(Button, self).__init__()
        self.setAcceptDrops(True)

        self.setGeometry(QtCore.QRect(30, 40, 41, 41))
        self.setObjectName("btn_a1")

if __name__=='__main__':
    app = QtGui.QApplication(sys.argv)
    ex = Ui_Form()
    ex.show()
    sys.exit(app.exec_())

推荐答案

您的 Button 类需要稍作修改:

A slight modification is required in your Button class:

def __init__(self, text='', parent=None):
    super(Button, self).__init__('Text', parent=parent)
    ...

然后在 Ui_Form 中的 setupUi 方法中更改

Then in your setupUi method in Ui_Form change

self.btn_a1 = Button(Form)

self.btn_a1 = Button('Text', self)

UI 现在看起来像这样:

The UI now looks like this:

这篇关于自定义按钮 PyQT4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆