在 QT Designer 中制作的响应能力不起作用。 Python
Responsiveness, made in QT Designer, is not working. Python
我在 QT Designer 中设计了一个应用程序并将其保存为 .ui
文件。
接下来,使用此命令将 .ui
文件转换为 .py
文件 - pyuic5 -o design.py responsive_design.ui
在此之后,添加此
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
在文件末尾使其工作。
但是,不幸的是,当我的 Python 文件启动时,应用程序以静态设计打开,没有像预期的那样响应。
我看到这个 -
是QT的bug还是我的错误?是否可以解决此问题?
我的完整代码如下 -
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'responsive_design.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 450)
MainWindow.setMinimumSize(QtCore.QSize(800, 450))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 801, 451))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../Downloads/bg.png"))
self.label.setScaledContents(True)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(-1, 0, 801, 451))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_2.setText("")
self.label_2.setPixmap(QtGui.QPixmap(""))
self.label_2.setScaledContents(False)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label_3 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_3.setText("")
self.label_3.setPixmap(QtGui.QPixmap(""))
self.label_3.setScaledContents(False)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.horizontalLayout.addWidget(self.label_3)
self.horizontalLayout.setStretch(0, 1)
self.horizontalLayout.setStretch(1, 4)
self.horizontalLayout.setStretch(2, 1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_5 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.horizontalLayout_2.addWidget(self.label_5)
self.label_6 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(16)
self.label_6.setFont(font)
self.label_6.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 255, 255, 236), stop:1 rgba(255, 255, 255, 220));\n"
"border-radius: 10px;")
self.label_6.setAlignment(QtCore.Qt.AlignCenter)
self.label_6.setObjectName("label_6")
self.horizontalLayout_2.addWidget(self.label_6)
self.label_4 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.horizontalLayout_2.addWidget(self.label_4)
self.horizontalLayout_2.setStretch(0, 1)
self.horizontalLayout_2.setStretch(1, 5)
self.horizontalLayout_2.setStretch(2, 1)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_7 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_7.setText("")
self.label_7.setObjectName("label_7")
self.horizontalLayout_3.addWidget(self.label_7)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.label_11 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(7)
self.label_11.setFont(font)
self.label_11.setAlignment(QtCore.Qt.AlignCenter)
self.label_11.setObjectName("label_11")
self.gridLayout.addWidget(self.label_11, 2, 0, 1, 1)
self.label_10 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_10.setToolTip("")
self.label_10.setWhatsThis("")
self.label_10.setText("")
self.label_10.setPixmap(QtGui.QPixmap("../microphone.png"))
self.label_10.setAlignment(QtCore.Qt.AlignCenter)
self.label_10.setObjectName("label_10")
self.gridLayout.addWidget(self.label_10, 1, 0, 1, 1)
self.label_9 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_9.setText("")
self.label_9.setAlignment(QtCore.Qt.AlignCenter)
self.label_9.setObjectName("label_9")
self.gridLayout.addWidget(self.label_9, 0, 0, 1, 1)
self.horizontalLayout_3.addLayout(self.gridLayout)
self.label_8 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_8.setText("")
self.label_8.setObjectName("label_8")
self.horizontalLayout_3.addWidget(self.label_8)
self.horizontalLayout_3.setStretch(0, 5)
self.horizontalLayout_3.setStretch(1, 4)
self.horizontalLayout_3.setStretch(2, 5)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.label_14 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_14.setText("")
self.label_14.setObjectName("label_14")
self.horizontalLayout_4.addWidget(self.label_14)
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.label_16 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_16.setText("")
self.label_16.setObjectName("label_16")
self.gridLayout_2.addWidget(self.label_16, 2, 0, 1, 1)
self.label_15 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(12)
self.label_15.setFont(font)
self.label_15.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 255, 255, 236), stop:1 rgba(255, 255, 255, 220));\n"
"border-radius: 10px;")
self.label_15.setAlignment(QtCore.Qt.AlignCenter)
self.label_15.setObjectName("label_15")
self.gridLayout_2.addWidget(self.label_15, 1, 0, 1, 1)
self.label_13 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(7)
self.label_13.setFont(font)
self.label_13.setAlignment(QtCore.Qt.AlignCenter)
self.label_13.setObjectName("label_13")
self.gridLayout_2.addWidget(self.label_13, 0, 0, 1, 1)
self.gridLayout_2.setRowStretch(0, 1)
self.gridLayout_2.setRowStretch(1, 3)
self.horizontalLayout_4.addLayout(self.gridLayout_2)
self.label_12 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_12.setText("")
self.label_12.setObjectName("label_12")
self.horizontalLayout_4.addWidget(self.label_12)
self.horizontalLayout_4.setStretch(0, 4)
self.horizontalLayout_4.setStretch(1, 5)
self.horizontalLayout_4.setStretch(2, 4)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.verticalLayout.setStretch(0, 2)
self.verticalLayout.setStretch(1, 8)
self.verticalLayout.setStretch(2, 2)
self.verticalLayout.setStretch(3, 6)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_6.setText(_translate("MainWindow", "Output"))
self.label_11.setText(_translate("MainWindow", "I'm listening when microphone is visible"))
self.label_15.setText(_translate("MainWindow", "SAID"))
self.label_13.setText(_translate("MainWindow", "I Think You Said:"))
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
首先我安装了 centrallayout,然后我将所有元素加载到布局中,然后我将这些布局加载到 centrallayout。
检查差异并自行编辑
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'responsive_design.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox, QVBoxLayout, QScrollArea, QWidget
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 450)
MainWindow.setMinimumSize(QtCore.QSize(800, 450))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel()
self.label.setPixmap(QtGui.QPixmap("../Downloads/bg.png"))
self.label.setScaledContents(True)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.centralLayout = QVBoxLayout(self.centralwidget)
self.centralLayout.setContentsMargins(0,0,0,0)
self.centralLayout.addWidget(self.label)
self.verticalLayout = QtWidgets.QVBoxLayout(self.label)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_2 = QtWidgets.QLabel()
self.label_2.setText("")
self.label_2.setPixmap(QtGui.QPixmap(""))
self.label_2.setScaledContents(False)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label_3 = QtWidgets.QLabel()
self.label_3.setText("")
self.label_3.setPixmap(QtGui.QPixmap(""))
self.label_3.setScaledContents(False)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.horizontalLayout.addWidget(self.label_3)
self.horizontalLayout.setStretch(0, 1)
self.horizontalLayout.setStretch(1, 4)
self.horizontalLayout.setStretch(2, 1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_5 = QtWidgets.QLabel()
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.horizontalLayout_2.addWidget(self.label_5)
self.label_6 = QtWidgets.QLabel()
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(16)
self.label_6.setFont(font)
self.label_6.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 255, 255, 236), stop:1 rgba(255, 255, 255, 220));\n"
"border-radius: 10px;")
self.label_6.setAlignment(QtCore.Qt.AlignCenter)
self.label_6.setObjectName("label_6")
self.horizontalLayout_2.addWidget(self.label_6)
self.label_4 = QtWidgets.QLabel()
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.horizontalLayout_2.addWidget(self.label_4)
self.horizontalLayout_2.setStretch(0, 1)
self.horizontalLayout_2.setStretch(1, 5)
self.horizontalLayout_2.setStretch(2, 1)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_7 = QtWidgets.QLabel()
self.label_7.setText("")
self.label_7.setObjectName("label_7")
self.horizontalLayout_3.addWidget(self.label_7)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.label_11 = QtWidgets.QLabel()
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(7)
self.label_11.setFont(font)
self.label_11.setAlignment(QtCore.Qt.AlignCenter)
self.label_11.setObjectName("label_11")
self.gridLayout.addWidget(self.label_11, 2, 0, 1, 1)
self.label_10 = QtWidgets.QLabel()
self.label_10.setToolTip("")
self.label_10.setWhatsThis("")
self.label_10.setText("")
self.label_10.setPixmap(QtGui.QPixmap("../microphone.png"))
self.label_10.setAlignment(QtCore.Qt.AlignCenter)
self.label_10.setObjectName("label_10")
self.gridLayout.addWidget(self.label_10, 1, 0, 1, 1)
self.label_9 = QtWidgets.QLabel()
self.label_9.setText("")
self.label_9.setAlignment(QtCore.Qt.AlignCenter)
self.label_9.setObjectName("label_9")
self.gridLayout.addWidget(self.label_9, 0, 0, 1, 1)
self.horizontalLayout_3.addLayout(self.gridLayout)
self.label_8 = QtWidgets.QLabel()
self.label_8.setText("")
self.label_8.setObjectName("label_8")
self.horizontalLayout_3.addWidget(self.label_8)
self.horizontalLayout_3.setStretch(0, 5)
self.horizontalLayout_3.setStretch(1, 4)
self.horizontalLayout_3.setStretch(2, 5)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.label_14 = QtWidgets.QLabel()
self.label_14.setText("")
self.label_14.setObjectName("label_14")
self.horizontalLayout_4.addWidget(self.label_14)
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.label_16 = QtWidgets.QLabel()
self.label_16.setText("")
self.label_16.setObjectName("label_16")
self.gridLayout_2.addWidget(self.label_16, 2, 0, 1, 1)
self.label_15 = QtWidgets.QLabel()
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(12)
self.label_15.setFont(font)
self.label_15.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 255, 255, 236), stop:1 rgba(255, 255, 255, 220));\n"
"border-radius: 10px;")
self.label_15.setAlignment(QtCore.Qt.AlignCenter)
self.label_15.setObjectName("label_15")
self.gridLayout_2.addWidget(self.label_15, 1, 0, 1, 1)
self.label_13 = QtWidgets.QLabel()
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(7)
self.label_13.setFont(font)
self.label_13.setAlignment(QtCore.Qt.AlignCenter)
self.label_13.setObjectName("label_13")
self.gridLayout_2.addWidget(self.label_13, 0, 0, 1, 1)
self.gridLayout_2.setRowStretch(0, 1)
self.gridLayout_2.setRowStretch(1, 3)
self.horizontalLayout_4.addLayout(self.gridLayout_2)
self.label_12 = QtWidgets.QLabel()
self.label_12.setText("")
self.label_12.setObjectName("label_12")
self.horizontalLayout_4.addWidget(self.label_12)
self.horizontalLayout_4.setStretch(0, 4)
self.horizontalLayout_4.setStretch(1, 5)
self.horizontalLayout_4.setStretch(2, 4)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.verticalLayout.setStretch(0, 2)
self.verticalLayout.setStretch(1, 8)
self.verticalLayout.setStretch(2, 2)
self.verticalLayout.setStretch(3, 6)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_6.setText(_translate("MainWindow", "Output"))
self.label_11.setText(_translate("MainWindow", "I'm listening when microphone is visible"))
self.label_15.setText(_translate("MainWindow", "SAID"))
self.label_13.setText(_translate("MainWindow", "I Think You Said:"))
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我在 QT Designer 中设计了一个应用程序并将其保存为 .ui
文件。
接下来,使用此命令将 .ui
文件转换为 .py
文件 - pyuic5 -o design.py responsive_design.ui
在此之后,添加此
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
在文件末尾使其工作。
但是,不幸的是,当我的 Python 文件启动时,应用程序以静态设计打开,没有像预期的那样响应。
我看到这个 -
是QT的bug还是我的错误?是否可以解决此问题?
我的完整代码如下 -
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'responsive_design.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 450)
MainWindow.setMinimumSize(QtCore.QSize(800, 450))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 801, 451))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../Downloads/bg.png"))
self.label.setScaledContents(True)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(-1, 0, 801, 451))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_2.setText("")
self.label_2.setPixmap(QtGui.QPixmap(""))
self.label_2.setScaledContents(False)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label_3 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_3.setText("")
self.label_3.setPixmap(QtGui.QPixmap(""))
self.label_3.setScaledContents(False)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.horizontalLayout.addWidget(self.label_3)
self.horizontalLayout.setStretch(0, 1)
self.horizontalLayout.setStretch(1, 4)
self.horizontalLayout.setStretch(2, 1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_5 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.horizontalLayout_2.addWidget(self.label_5)
self.label_6 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(16)
self.label_6.setFont(font)
self.label_6.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 255, 255, 236), stop:1 rgba(255, 255, 255, 220));\n"
"border-radius: 10px;")
self.label_6.setAlignment(QtCore.Qt.AlignCenter)
self.label_6.setObjectName("label_6")
self.horizontalLayout_2.addWidget(self.label_6)
self.label_4 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.horizontalLayout_2.addWidget(self.label_4)
self.horizontalLayout_2.setStretch(0, 1)
self.horizontalLayout_2.setStretch(1, 5)
self.horizontalLayout_2.setStretch(2, 1)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_7 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_7.setText("")
self.label_7.setObjectName("label_7")
self.horizontalLayout_3.addWidget(self.label_7)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.label_11 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(7)
self.label_11.setFont(font)
self.label_11.setAlignment(QtCore.Qt.AlignCenter)
self.label_11.setObjectName("label_11")
self.gridLayout.addWidget(self.label_11, 2, 0, 1, 1)
self.label_10 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_10.setToolTip("")
self.label_10.setWhatsThis("")
self.label_10.setText("")
self.label_10.setPixmap(QtGui.QPixmap("../microphone.png"))
self.label_10.setAlignment(QtCore.Qt.AlignCenter)
self.label_10.setObjectName("label_10")
self.gridLayout.addWidget(self.label_10, 1, 0, 1, 1)
self.label_9 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_9.setText("")
self.label_9.setAlignment(QtCore.Qt.AlignCenter)
self.label_9.setObjectName("label_9")
self.gridLayout.addWidget(self.label_9, 0, 0, 1, 1)
self.horizontalLayout_3.addLayout(self.gridLayout)
self.label_8 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_8.setText("")
self.label_8.setObjectName("label_8")
self.horizontalLayout_3.addWidget(self.label_8)
self.horizontalLayout_3.setStretch(0, 5)
self.horizontalLayout_3.setStretch(1, 4)
self.horizontalLayout_3.setStretch(2, 5)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.label_14 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_14.setText("")
self.label_14.setObjectName("label_14")
self.horizontalLayout_4.addWidget(self.label_14)
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.label_16 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_16.setText("")
self.label_16.setObjectName("label_16")
self.gridLayout_2.addWidget(self.label_16, 2, 0, 1, 1)
self.label_15 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(12)
self.label_15.setFont(font)
self.label_15.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 255, 255, 236), stop:1 rgba(255, 255, 255, 220));\n"
"border-radius: 10px;")
self.label_15.setAlignment(QtCore.Qt.AlignCenter)
self.label_15.setObjectName("label_15")
self.gridLayout_2.addWidget(self.label_15, 1, 0, 1, 1)
self.label_13 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(7)
self.label_13.setFont(font)
self.label_13.setAlignment(QtCore.Qt.AlignCenter)
self.label_13.setObjectName("label_13")
self.gridLayout_2.addWidget(self.label_13, 0, 0, 1, 1)
self.gridLayout_2.setRowStretch(0, 1)
self.gridLayout_2.setRowStretch(1, 3)
self.horizontalLayout_4.addLayout(self.gridLayout_2)
self.label_12 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_12.setText("")
self.label_12.setObjectName("label_12")
self.horizontalLayout_4.addWidget(self.label_12)
self.horizontalLayout_4.setStretch(0, 4)
self.horizontalLayout_4.setStretch(1, 5)
self.horizontalLayout_4.setStretch(2, 4)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.verticalLayout.setStretch(0, 2)
self.verticalLayout.setStretch(1, 8)
self.verticalLayout.setStretch(2, 2)
self.verticalLayout.setStretch(3, 6)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_6.setText(_translate("MainWindow", "Output"))
self.label_11.setText(_translate("MainWindow", "I'm listening when microphone is visible"))
self.label_15.setText(_translate("MainWindow", "SAID"))
self.label_13.setText(_translate("MainWindow", "I Think You Said:"))
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
首先我安装了 centrallayout,然后我将所有元素加载到布局中,然后我将这些布局加载到 centrallayout。
检查差异并自行编辑
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'responsive_design.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox, QVBoxLayout, QScrollArea, QWidget
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 450)
MainWindow.setMinimumSize(QtCore.QSize(800, 450))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel()
self.label.setPixmap(QtGui.QPixmap("../Downloads/bg.png"))
self.label.setScaledContents(True)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.centralLayout = QVBoxLayout(self.centralwidget)
self.centralLayout.setContentsMargins(0,0,0,0)
self.centralLayout.addWidget(self.label)
self.verticalLayout = QtWidgets.QVBoxLayout(self.label)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_2 = QtWidgets.QLabel()
self.label_2.setText("")
self.label_2.setPixmap(QtGui.QPixmap(""))
self.label_2.setScaledContents(False)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label_3 = QtWidgets.QLabel()
self.label_3.setText("")
self.label_3.setPixmap(QtGui.QPixmap(""))
self.label_3.setScaledContents(False)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.horizontalLayout.addWidget(self.label_3)
self.horizontalLayout.setStretch(0, 1)
self.horizontalLayout.setStretch(1, 4)
self.horizontalLayout.setStretch(2, 1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_5 = QtWidgets.QLabel()
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.horizontalLayout_2.addWidget(self.label_5)
self.label_6 = QtWidgets.QLabel()
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(16)
self.label_6.setFont(font)
self.label_6.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 255, 255, 236), stop:1 rgba(255, 255, 255, 220));\n"
"border-radius: 10px;")
self.label_6.setAlignment(QtCore.Qt.AlignCenter)
self.label_6.setObjectName("label_6")
self.horizontalLayout_2.addWidget(self.label_6)
self.label_4 = QtWidgets.QLabel()
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.horizontalLayout_2.addWidget(self.label_4)
self.horizontalLayout_2.setStretch(0, 1)
self.horizontalLayout_2.setStretch(1, 5)
self.horizontalLayout_2.setStretch(2, 1)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_7 = QtWidgets.QLabel()
self.label_7.setText("")
self.label_7.setObjectName("label_7")
self.horizontalLayout_3.addWidget(self.label_7)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.label_11 = QtWidgets.QLabel()
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(7)
self.label_11.setFont(font)
self.label_11.setAlignment(QtCore.Qt.AlignCenter)
self.label_11.setObjectName("label_11")
self.gridLayout.addWidget(self.label_11, 2, 0, 1, 1)
self.label_10 = QtWidgets.QLabel()
self.label_10.setToolTip("")
self.label_10.setWhatsThis("")
self.label_10.setText("")
self.label_10.setPixmap(QtGui.QPixmap("../microphone.png"))
self.label_10.setAlignment(QtCore.Qt.AlignCenter)
self.label_10.setObjectName("label_10")
self.gridLayout.addWidget(self.label_10, 1, 0, 1, 1)
self.label_9 = QtWidgets.QLabel()
self.label_9.setText("")
self.label_9.setAlignment(QtCore.Qt.AlignCenter)
self.label_9.setObjectName("label_9")
self.gridLayout.addWidget(self.label_9, 0, 0, 1, 1)
self.horizontalLayout_3.addLayout(self.gridLayout)
self.label_8 = QtWidgets.QLabel()
self.label_8.setText("")
self.label_8.setObjectName("label_8")
self.horizontalLayout_3.addWidget(self.label_8)
self.horizontalLayout_3.setStretch(0, 5)
self.horizontalLayout_3.setStretch(1, 4)
self.horizontalLayout_3.setStretch(2, 5)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.label_14 = QtWidgets.QLabel()
self.label_14.setText("")
self.label_14.setObjectName("label_14")
self.horizontalLayout_4.addWidget(self.label_14)
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.label_16 = QtWidgets.QLabel()
self.label_16.setText("")
self.label_16.setObjectName("label_16")
self.gridLayout_2.addWidget(self.label_16, 2, 0, 1, 1)
self.label_15 = QtWidgets.QLabel()
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(12)
self.label_15.setFont(font)
self.label_15.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 255, 255, 236), stop:1 rgba(255, 255, 255, 220));\n"
"border-radius: 10px;")
self.label_15.setAlignment(QtCore.Qt.AlignCenter)
self.label_15.setObjectName("label_15")
self.gridLayout_2.addWidget(self.label_15, 1, 0, 1, 1)
self.label_13 = QtWidgets.QLabel()
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(7)
self.label_13.setFont(font)
self.label_13.setAlignment(QtCore.Qt.AlignCenter)
self.label_13.setObjectName("label_13")
self.gridLayout_2.addWidget(self.label_13, 0, 0, 1, 1)
self.gridLayout_2.setRowStretch(0, 1)
self.gridLayout_2.setRowStretch(1, 3)
self.horizontalLayout_4.addLayout(self.gridLayout_2)
self.label_12 = QtWidgets.QLabel()
self.label_12.setText("")
self.label_12.setObjectName("label_12")
self.horizontalLayout_4.addWidget(self.label_12)
self.horizontalLayout_4.setStretch(0, 4)
self.horizontalLayout_4.setStretch(1, 5)
self.horizontalLayout_4.setStretch(2, 4)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.verticalLayout.setStretch(0, 2)
self.verticalLayout.setStretch(1, 8)
self.verticalLayout.setStretch(2, 2)
self.verticalLayout.setStretch(3, 6)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_6.setText(_translate("MainWindow", "Output"))
self.label_11.setText(_translate("MainWindow", "I'm listening when microphone is visible"))
self.label_15.setText(_translate("MainWindow", "SAID"))
self.label_13.setText(_translate("MainWindow", "I Think You Said:"))
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())