在所有代码都出现错误之前:为什么需要从 QLabel和QPixmap?我不明白重点,QLabela QPixmap(合成),QLabel示例:新标签.py**
from PyQt5 import QtCore, QtWidgets
class NeuLabel(QtWidgets.QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.setAcceptDrops(True)
def mousePressEvent(self, e):
super().mousePressEvent(e)
if e.button() == QtCore.Qt.LeftButton:
print("press")
然后在.ui中,您必须在表单中填写以下内容,按“添加”按钮,然后是“升级”按钮
正在生成以下.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="NeuLabel" name="label">
<property name="geometry">
<rect>
<x>120</x>
<y>160</y>
<width>251</width>
<height>191</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>NeuLabel</class>
<extends>QLabel</extends>
<header>neulabel</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
Then used in the main file:
import os
import sys
from PyQt5 import QtWidgets, uic
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
uifile_1 = os.path.join(CURRENT_DIR, "testpromote.ui")
form_1, base_1 = uic.loadUiType(uifile_1)
class myApp(base_1, form_1):
def __init__(self):
super(base_1, self).__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = myApp()
ex.show()
sys.exit(app.exec_())
├── main.py
├── neulabel.py
└── testpromote.ui