自定义按钮PyQT4
Custom button PyQT4
我无法显示我在 PyQt4 中使用 Python 2.7 从 class 创建的按钮。我的项目有 80 个按钮,它们都需要拖放功能。我创建了一个允许此操作的按钮 class,但我不确定如何在 Ui_Form
中显示它。
我认为我不能使用布局管理器,因为我想要背景图像,所以我假设我需要使用 Form
中的 Form
参数从 Button class 创建按钮11=] 的 setupUi
功能,但我确定如何。
下面是一个只有两个按钮(和屏幕截图)的脚本示例 - 一个是从我的 class 调用的,另一个是 QT Designer 留下的。按钮 btn_a1
应该在 btn_a2
的左侧可见,但它不是(因为不知道如何正确调用它)。我删除了按钮的标签和功能代码,以帮助提高易用性。
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
class:
中需要稍作修改
def __init__(self, text='', parent=None):
super(Button, self).__init__('Text', parent=parent)
...
然后在 Ui_Form
中的 setupUi
方法中更改
self.btn_a1 = Button(Form)
至
self.btn_a1 = Button('Text', self)
UI 现在看起来像这样:
我无法显示我在 PyQt4 中使用 Python 2.7 从 class 创建的按钮。我的项目有 80 个按钮,它们都需要拖放功能。我创建了一个允许此操作的按钮 class,但我不确定如何在 Ui_Form
中显示它。
我认为我不能使用布局管理器,因为我想要背景图像,所以我假设我需要使用 Form
中的 Form
参数从 Button class 创建按钮11=] 的 setupUi
功能,但我确定如何。
下面是一个只有两个按钮(和屏幕截图)的脚本示例 - 一个是从我的 class 调用的,另一个是 QT Designer 留下的。按钮 btn_a1
应该在 btn_a2
的左侧可见,但它不是(因为不知道如何正确调用它)。我删除了按钮的标签和功能代码,以帮助提高易用性。
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
class:
def __init__(self, text='', parent=None):
super(Button, self).__init__('Text', parent=parent)
...
然后在 Ui_Form
中的 setupUi
方法中更改
self.btn_a1 = Button(Form)
至
self.btn_a1 = Button('Text', self)
UI 现在看起来像这样: