我有一个pyQt脚本(在终端中用pyuic5创建),它创建了一个带有一些功能的窗口。这个工作和脚本如下:
from PyQt5 import QtCore, QtGui, QtWidgets
from Main import Ui_Main
from resources.icons import *
import sys
class MainW(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainW, self).__init__(parent) #pyQt5
self.ui = Ui_Main()
self.ui.setupUi(self)
self.ui.setupUi
self.ui.retranslateUi(self)
# self.ui.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.ui.setWindowTitle("Meas Sound Measurement Tool")
self.ui.progressBar.setProperty("value", 1)
# measExit = self.ui.menuJBae_Meas.addAction(self.ui.actionExit)
# measExit.triggered.connect(self.Exit)
def Exit(self):
choice = QtGui.QMessageBox.question(self, 'Exit Meas',
"Are You sure to Leave Meas?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if choice == QtGui.QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainapp = MainW()
mainapp.show()
sys.exit(app.exec_())
此后问题出现了。我想创建一些功能。我从菜单中的退出功能开始
我无法将退出脚本链接到脚本。我认为这是因为标准文件有一个对象main
,其中所有子对象都与之相关。
我无法弄清楚如何将我的脚本(现在是上面的脚本)与pyuic5的标准脚本(下面的脚本)联系起来
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Main(object):
def setupUi(self, Main):
Main.setObjectName("Main")
Main.resize(885, 600)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(Main.sizePolicy().hasHeightForWidth())
Main.setSizePolicy(sizePolicy)
Main.setStyleSheet("font: 9pt \"SansSerif\";")
self.Central = QtWidgets.QWidget(Main)
self.Central.setObjectName("Central")
self.Logo = QtWidgets.QGraphicsView(self.Central)
self.Logo.setGeometry(QtCore.QRect(10, 10, 240, 121))
self.Logo.setMinimumSize(QtCore.QSize(240, 120))
self.Logo.setMaximumSize(QtCore.QSize(16777215, 350))
self.Logo.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedKingdom))
self.Logo.setFrameShadow(QtWidgets.QFrame.Raised)
self.Logo.setObjectName("Logo")
self.test = QtWidgets.QToolButton(self.Central)
self.test.setGeometry(QtCore.QRect(10, 520, 80, 23))
self.test.setObjectName("test")
self.start = QtWidgets.QToolButton(self.Central)
self.start.setGeometry(QtCore.QRect(110, 520, 131, 23))
self.start.setObjectName("start")
self.layoutWidget = QtWidgets.QWidget(self.Central)
self.layoutWidget.setGeometry(QtCore.QRect(260, 520, 521, 27))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.progressBar = QtWidgets.QProgressBar(self.layoutWidget)
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.horizontalLayout.addWidget(self.progressBar)
self.dateTimeEdit = QtWidgets.QDateTimeEdit(self.layoutWidget)
self.dateTimeEdit.setObjectName("dateTimeEdit")
self.horizontalLayout.addWidget(self.dateTimeEdit)
self.plotTop = QtWidgets.QGraphicsView(self.Central)
self.plotTop.setGeometry(QtCore.QRect(260, 10, 521, 240))
self.plotTop.setMinimumSize(QtCore.QSize(0, 240))
self.plotTop.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.plotTop.setFrameShadow(QtWidgets.QFrame.Sunken)
self.plotTop.setObjectName("plotTop")
self.PlotBottom = QtWidgets.QGraphicsView(self.Central)
self.PlotBottom.setGeometry(QtCore.QRect(260, 260, 521, 240))
self.PlotBottom.setMinimumSize(QtCore.QSize(0, 240))
self.PlotBottom.setObjectName("PlotBottom")
self.layoutWidget1 = QtWidgets.QWidget(self.Central)
self.layoutWidget1.setGeometry(QtCore.QRect(800, 10, 28, 231))
self.layoutWidget1.setObjectName("layoutWidget1")
self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget1)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
spacerItem = QtWidgets.QSpacerItem(20, 180, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.ViewPropTop = QtWidgets.QToolButton(self.layoutWidget1)
self.ViewPropTop.setObjectName("ViewPropTop")
self.verticalLayout.addWidget(self.ViewPropTop)
self.zoomTop = QtWidgets.QToolButton(self.layoutWidget1)
self.zoomTop.setObjectName("zoomTop")
self.verticalLayout.addWidget(self.zoomTop)
self.toolTop = QtWidgets.QToolButton(self.layoutWidget1)
self.toolTop.setObjectName("toolTop")
self.verticalLayout.addWidget(self.toolTop)
self.layoutWidget2 = QtWidgets.QWidget(self.Central)
self.layoutWidget2.setGeometry(QtCore.QRect(800, 260, 28, 231))
self.layoutWidget2.setObjectName("layoutWidget2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.layoutWidget2)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
spacerItem1 = QtWidgets.QSpacerItem(20, 180, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout_2.addItem(spacerItem1)
self.ViewPropBBottom = QtWidgets.QToolButton(self.layoutWidget2)
self.ViewPropBBottom.setObjectName("ViewPropBBottom")
self.verticalLayout_2.addWidget(self.ViewPropBBottom)
self.ZoomBottom = QtWidgets.QToolButton(self.layoutWidget2)
self.ZoomBottom.setObjectName("ZoomBottom")
self.verticalLayout_2.addWidget(self.ZoomBottom)
self.toolBottom = QtWidgets.QToolButton(self.layoutWidget2)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("../resources/icons/WrenchSm.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
self.toolBottom.setIcon(icon)
self.toolBottom.setObjectName("toolBottom")
self.verticalLayout_2.addWidget(self.toolBottom)
self.layoutWidget3 = QtWidgets.QWidget(self.Central)
self.layoutWidget3.setGeometry(QtCore.QRect(10, 170, 231, 271))
self.layoutWidget3.setObjectName("layoutWidget3")
self.gridLayout_3 = QtWidgets.QGridLayout(self.layoutWidget3)
self.gridLayout_3.setContentsMargins(0, 0, 0, 0)
self.gridLayout_3.setObjectName("gridLayout_3")
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.TypeLab = QtWidgets.QLabel(self.layoutWidget3)
self.TypeLab.setObjectName("TypeLab")
self.verticalLayout_3.addWidget(self.TypeLab)
self.TypeSel = QtWidgets.QComboBox(self.layoutWidget3)
self.TypeSel.setMinimumSize(QtCore.QSize(140, 0))
self.TypeSel.setStatusTip("")
self.TypeSel.setObjectName("TypeSel")
self.verticalLayout_3.addWidget(self.TypeSel)
self.gridLayout_3.addLayout(self.verticalLayout_3, 0, 0, 2, 1)
self.label = QtWidgets.QLabel(self.layoutWidget3)
self.label.setObjectName("label")
self.gridLayout_3.addWidget(self.label, 0, 1, 1, 1)
self.Level = QtWidgets.QDial(self.layoutWidget3)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.Level.sizePolicy().hasHeightForWidth())
self.Level.setSizePolicy(sizePolicy)
self.Level.setMinimumSize(QtCore.QSize(75, 75))
self.Level.setStatusTip("")
self.Level.setAccessibleDescription("")
self.Level.setObjectName("Level")
self.gridLayout_3.addWidget(self.Level, 1, 1, 2, 1)
self.verticalLayout_4 = QtWidgets.QVBoxLayout()
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.lengthLab = QtWidgets.QLabel(self.layoutWidget3)
self.lengthLab.setObjectName("lengthLab")
self.verticalLayout_4.addWidget(self.lengthLab)
self.length = QtWidgets.QComboBox(self.layoutWidget3)
self.length.setMinimumSize(QtCore.QSize(140, 0))
self.length.setObjectName("length")
self.verticalLayout_4.addWidget(self.length)
self.gridLayout_3.addLayout(self.verticalLayout_4, 2, 0, 1, 1)
self.verticalLayout_5 = QtWidgets.QVBoxLayout()
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.itterationsLab = QtWidgets.QLabel(self.layoutWidget3)
self.itterationsLab.setObjectName("itterationsLab")
self.verticalLayout_5.addWidget(self.itterationsLab)
self.Itterations = QtWidgets.QComboBox(self.layoutWidget3)
self.Itterations.setMinimumSize(QtCore.QSize(140, 0))
self.Itterations.setAccessibleDescription("")
self.Itterations.setObjectName("Itterations")
self.verticalLayout_5.addWidget(self.Itterations)
self.gridLayout_3.addLayout(self.verticalLayout_5, 3, 0, 1, 1)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.startLab = QtWidgets.QLabel(self.layoutWidget3)
self.startLab.setObjectName("startLab")
self.gridLayout.addWidget(self.startLab, 0, 0, 1, 1)
self.startSlid = QtWidgets.QSlider(self.layoutWidget3)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.startSlid.sizePolicy().hasHeightForWidth())
self.startSlid.setSizePolicy(sizePolicy)
self.startSlid.setMinimumSize(QtCore.QSize(140, 0))
self.startSlid.setMaximum(100000)
self.startSlid.setOrientation(QtCore.Qt.Horizontal)
self.startSlid.setObjectName("startSlid")
self.gridLayout.addWidget(self.startSlid, 1, 0, 1, 1)
self.startSpin = QtWidgets.QDoubleSpinBox(self.layoutWidget3)
self.startSpin.setMinimumSize(QtCore.QSize(65, 0))
self.startSpin.setDecimals(0)
self.startSpin.setMaximum(100000.0)
self.startSpin.setProperty("value", 1.0)
self.startSpin.setObjectName("startSpin")
self.gridLayout.addWidget(self.startSpin, 1, 1, 1, 1)
self.gridLayout_3.addLayout(self.gridLayout, 4, 0, 1, 2)
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.endLab = QtWidgets.QLabel(self.layoutWidget3)
self.endLab.setObjectName("endLab")
self.gridLayout_2.addWidget(self.endLab, 0, 0, 1, 1)
self.endSlid = QtWidgets.QSlider(self.layoutWidget3)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.endSlid.sizePolicy().hasHeightForWidth())
self.endSlid.setSizePolicy(sizePolicy)
self.endSlid.setMinimumSize(QtCore.QSize(140, 0))
self.endSlid.setMaximum(100000)
self.endSlid.setOrientation(QtCore.Qt.Horizontal)
self.endSlid.setObjectName("endSlid")
self.gridLayout_2.addWidget(self.endSlid, 1, 0, 1, 1)
self.endSpin = QtWidgets.QDoubleSpinBox(self.layoutWidget3)
self.endSpin.setMinimumSize(QtCore.QSize(65, 0))
self.endSpin.setDecimals(0)
self.endSpin.setMaximum(100000.0)
self.endSpin.setProperty("value", 100000.0)
self.endSpin.setObjectName("endSpin")
self.gridLayout_2.addWidget(self.endSpin, 1, 1, 1, 1)
self.gridLayout_3.addLayout(self.gridLayout_2, 5, 0, 1, 2)
self.layoutWidget.raise_()
self.Logo.raise_()
self.test.raise_()
self.start.raise_()
self.plotTop.raise_()
self.layoutWidget.raise_()
self.PlotBottom.raise_()
self.layoutWidget.raise_()
self.layoutWidget.raise_()
Main.setCentralWidget(self.Central)
self.menubar = QtWidgets.QMenuBar(Main)
self.menubar.setGeometry(QtCore.QRect(0, 0, 885, 20))
self.menubar.setObjectName("menubar")
self.menuJBae_Meas = QtWidgets.QMenu(self.menubar)
self.menuJBae_Meas.setObjectName("menuJBae_Meas")
self.menuEdit = QtWidgets.QMenu(self.menubar)
self.menuEdit.setObjectName("menuEdit")
Main.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Main)
self.statusbar.setObjectName("statusbar")
Main.setStatusBar(self.statusbar)
self.actionExit = QtWidgets.QAction(Main)
self.actionExit.setEnabled(True)
self.actionExit.setObjectName("actionExit")
self.actionSave = QtWidgets.QAction(Main)
self.actionSave.setObjectName("actionSave")
self.actionSave_All = QtWidgets.QAction(Main)
self.actionSave_All.setObjectName("actionSave_All")
self.actionPreferences = QtWidgets.QAction(Main)
self.actionPreferences.setObjectName("actionPreferences")
self.actionSave_As = QtWidgets.QAction(Main)
self.actionSave_As.setObjectName("actionSave_As")
self.actionDelete_Measurement = QtWidgets.QAction(Main)
self.actionDelete_Measurement.setObjectName("actionDelete_Measurement")
self.actionOpen = QtWidgets.QAction(Main)
self.actionOpen.setObjectName("actionOpen")
self.actionNew = QtWidgets.QAction(Main)
self.actionNew.setObjectName("actionNew")
self.menuJBae_Meas.addAction(self.actionNew)
self.menuJBae_Meas.addAction(self.actionOpen)
self.menuJBae_Meas.addAction(self.actionSave)
self.menuJBae_Meas.addAction(self.actionSave_As)
self.menuJBae_Meas.addAction(self.actionSave_All)
self.menuJBae_Meas.addSeparator()
self.menuJBae_Meas.addAction(self.actionPreferences)
self.menuJBae_Meas.addAction(self.actionExit)
self.menuEdit.addAction(self.actionDelete_Measurement)
self.menubar.addAction(self.menuJBae_Meas.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.retranslateUi(Main)
QtCore.QMetaObject.connectSlotsByName(Main)
Main.setTabOrder(self.TypeSel, self.length)
Main.setTabOrder(self.length, self.Itterations)
Main.setTabOrder(self.Itterations, self.startSlid)
Main.setTabOrder(self.startSlid, self.startSpin)
Main.setTabOrder(self.startSpin, self.endSlid)
Main.setTabOrder(self.endSlid, self.endSpin)
Main.setTabOrder(self.endSpin, self.Level)
Main.setTabOrder(self.Level, self.start)
Main.setTabOrder(self.start, self.test)
Main.setTabOrder(self.test, self.ViewPropTop)
Main.setTabOrder(self.ViewPropTop, self.zoomTop)
Main.setTabOrder(self.zoomTop, self.toolTop)
Main.setTabOrder(self.toolTop, self.ViewPropBBottom)
Main.setTabOrder(self.ViewPropBBottom, self.ZoomBottom)
Main.setTabOrder(self.ZoomBottom, self.toolBottom)
Main.setTabOrder(self.toolBottom, self.plotTop)
Main.setTabOrder(self.plotTop, self.PlotBottom)
Main.setTabOrder(self.PlotBottom, self.dateTimeEdit)
Main.setTabOrder(self.dateTimeEdit, self.Logo)
def retranslateUi(self, Main):
_translate = QtCore.QCoreApplication.translate
Main.setWindowTitle(_translate("Main", "Main"))
self.test.setToolTip(_translate("Main", "Open Menu Test Signal"))
self.test.setText(_translate("Main", "Test"))
self.start.setToolTip(_translate("Main", "Start Measurement"))
self.start.setText(_translate("Main", "Start"))
self.ViewPropTop.setText(_translate("Main", "..."))
self.zoomTop.setText(_translate("Main", "..."))
self.toolTop.setText(_translate("Main", "..."))
self.ViewPropBBottom.setText(_translate("Main", "..."))
self.ZoomBottom.setText(_translate("Main", "..."))
self.toolBottom.setText(_translate("Main", "..."))
self.TypeLab.setText(_translate("Main", "Type"))
self.TypeSel.setToolTip(_translate("Main", "Select Signal Type"))
self.label.setText(_translate("Main", "Level"))
self.Level.setToolTip(_translate("Main", "Set Volume Level"))
self.lengthLab.setText(_translate("Main", "Length"))
self.length.setToolTip(_translate("Main", "Select Length of Signal in k samples"))
self.itterationsLab.setText(_translate("Main", "Itterations"))
self.Itterations.setToolTip(_translate("Main", "Select number of itterations"))
self.startLab.setText(_translate("Main", "Start"))
self.startSlid.setToolTip(_translate("Main", "Select Start Frequency"))
self.startSpin.setToolTip(_translate("Main", "Set Start Frequency"))
self.endLab.setText(_translate("Main", "End"))
self.endSlid.setToolTip(_translate("Main", "Select End Frequency"))
self.endSpin.setToolTip(_translate("Main", "Set End Frequency"))
self.menuJBae_Meas.setTitle(_translate("Main", "&File"))
self.menuEdit.setTitle(_translate("Main", "Edit"))
self.actionExit.setText(_translate("Main", "&Quit"))
self.actionExit.setIconText(_translate("Main", "Quit"))
self.actionExit.setToolTip(_translate("Main", "Exit Meas"))
self.actionExit.setShortcut(_translate("Main", "Ctrl+Q"))
self.actionSave.setText(_translate("Main", "&Save"))
self.actionSave.setIconText(_translate("Main", "&Save"))
self.actionSave.setToolTip(_translate("Main", "Save Measurements"))
self.actionSave.setShortcut(_translate("Main", "Ctrl+S"))
self.actionSave_All.setText(_translate("Main", "Save All"))
self.actionSave_All.setToolTip(_translate("Main", "Save All Measurements"))
self.actionSave_All.setShortcut(_translate("Main", "Ctrl+Shift+S"))
self.actionPreferences.setText(_translate("Main", "&Preferences"))
self.actionPreferences.setIconText(_translate("Main", "&Preferences"))
self.actionPreferences.setToolTip(_translate("Main", "Preferences Meas"))
self.actionPreferences.setShortcut(_translate("Main", "Ctrl+I"))
self.actionSave_As.setText(_translate("Main", "Save As"))
self.actionSave_As.setToolTip(_translate("Main", "Save Measurements As"))
self.actionDelete_Measurement.setText(_translate("Main", "Delete Measurement"))
self.actionOpen.setText(_translate("Main", "&Open"))
self.actionOpen.setIconText(_translate("Main", "&Open"))
self.actionOpen.setShortcut(_translate("Main", "Ctrl+O"))
self.actionNew.setText(_translate("Main", "&New"))
self.actionNew.setIconText(_translate("Main", "&New"))
self.actionNew.setShortcut(_translate("Main", "Ctrl+N"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Main = QtWidgets.QMainWindow()
ui = Ui_Main()
ui.setupUi(Main)
Main.show()
sys.exit(app.exec_())
第二个脚本如何使用main
中的对象?
答案 0 :(得分:0)
Qt Designer中添加的所有小部件都成为分配给self.ui
的对象的属性,这只是一个简单的命名空间。因此,您可以访问actionExit
并将其连接到插槽,如下所示:
class MainW(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainW, self).__init__(parent)
self.ui = Ui_Main()
self.ui.setupUi(self)
# self.ui.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.ui.setWindowTitle("Meas Sound Measurement Tool")
self.ui.progressBar.setProperty("value", 1)
self.ui.actionExit.triggered.connect(self.Exit)
但请注意Exit
方法有一些错误,应该是这样的:
def Exit(self):
choice = QtWidgets.QMessageBox.question(self, 'Exit Meas',
"Are You sure to Leave Meas?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if choice == QtWidgets.QMessageBox.Yes:
self.close()
如果您不想一直输入self.ui
,可以使用多继承方法简化一些事情:
class MainW(QtWidgets.QMainWindow, Ui_Main):
def __init__(self, parent=None):
super(MainW, self).__init__(parent)
self.setupUi(self)
# self.setWindowIcon(QtWidgets.QIcon('MeasLogoSm.png'))
# self.setWindowTitle("Meas Sound Measurement Tool")
self.progressBar.setProperty("value", 1)
self.actionExit.triggered.connect(self.Exit)
现在,Qt Designer中添加的所有小部件都是MainW
对象的直接属性。