PyQT5 Main window 导入语法

PyQT5 Main window import syntax

我将以下代码保存在名为 Main_Win.py 的文件中,该文件来自不应编辑的 PyQt5 设计。

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'Main_Win.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1500, 700)
        MainWindow.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("monkey.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.display_data = QtWidgets.QLabel(self.centralwidget)
        self.display_data.setGeometry(QtCore.QRect(50, 110, 1381, 361))
        self.display_data.setAlignment(QtCore.Qt.AlignCenter)
        self.display_data.setObjectName("display_data")
        self.pause_button = QtWidgets.QPushButton(self.centralwidget)
        self.pause_button.setGeometry(QtCore.QRect(490, 560, 131, 51))
        self.pause_button.setObjectName("pause_button")
        self.play_button = QtWidgets.QPushButton(self.centralwidget)
        self.play_button.setGeometry(QtCore.QRect(860, 560, 131, 51))
        self.play_button.setObjectName("play_button")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1500, 21))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        self.menuOptions = QtWidgets.QMenu(self.menubar)
        self.menuOptions.setObjectName("menuOptions")
        self.menuAbout = QtWidgets.QMenu(self.menubar)
        self.menuAbout.setObjectName("menuAbout")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionOpen_Learning_Package = QtWidgets.QAction(MainWindow)
        self.actionOpen_Learning_Package.setObjectName("actionOpen_Learning_Package")
        self.actionExit_Ctrl_Q = QtWidgets.QAction(MainWindow)
        self.actionExit_Ctrl_Q.setObjectName("actionExit_Ctrl_Q")
        self.actionChange_Font = QtWidgets.QAction(MainWindow)
        self.actionChange_Font.setObjectName("actionChange_Font")
        self.actionChange_Font_Color = QtWidgets.QAction(MainWindow)
        self.actionChange_Font_Color.setObjectName("actionChange_Font_Color")
        self.actionChange_Words_Per_Minute = QtWidgets.QAction(MainWindow)
        self.actionChange_Words_Per_Minute.setObjectName("actionChange_Words_Per_Minute")
        self.actionCreate_Learning_Package = QtWidgets.QAction(MainWindow)
        self.actionCreate_Learning_Package.setObjectName("actionCreate_Learning_Package")
        self.actionVersion_1_1 = QtWidgets.QAction(MainWindow)
        self.actionVersion_1_1.setObjectName("actionVersion_1_1")
        self.menuFile.addAction(self.actionOpen_Learning_Package)
        self.menuFile.addAction(self.actionExit_Ctrl_Q)
        self.menuOptions.addAction(self.actionChange_Font)
        self.menuOptions.addAction(self.actionChange_Font_Color)
        self.menuOptions.addAction(self.actionChange_Words_Per_Minute)
        self.menuOptions.addAction(self.actionCreate_Learning_Package)
        self.menuAbout.addAction(self.actionVersion_1_1)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuOptions.menuAction())
        self.menubar.addAction(self.menuAbout.menuAction())

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Ultra-Fast Conceptualizer"))
        self.display_data.setText(_translate("MainWindow", "TextLabel"))
        self.pause_button.setText(_translate("MainWindow", "Pause ||"))
        self.play_button.setText(_translate("MainWindow", "Play "))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.menuOptions.setTitle(_translate("MainWindow", "Options"))
        self.menuAbout.setTitle(_translate("MainWindow", "About"))
        self.actionOpen_Learning_Package.setText(_translate("MainWindow", "Open Learning Package"))
        self.actionExit_Ctrl_Q.setText(_translate("MainWindow", "Exit (Ctrl+Q)"))
        self.actionChange_Font.setText(_translate("MainWindow", "Change Font"))
        self.actionChange_Font_Color.setText(_translate("MainWindow", "Change Font Color"))
        self.actionChange_Words_Per_Minute.setText(_translate("MainWindow", "Change Words Per Minute"))
        self.actionCreate_Learning_Package.setText(_translate("MainWindow", "Create Learning Package"))
        self.actionVersion_1_1.setText(_translate("MainWindow", "Version 1.1"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

然后我有我的主应用程序文件,我试图从中导入 PyQt5 文件。下面的代码

from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from Main_Win import Ui_MainWindow



class MainWindow(qtw.QMainWindow):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        #self.ui = Ui_MainWindow()
        #self.ui.setupUi(self)

        self.play_button.clicked.connect(play_button_clicked)

        

    def play_button_clicked(self):

        print('woohoo this worked')
        sys.exit()

    
if __name__ == "__main__":
    import sys
    app = qtw.QApplication(sys.argv)
    MainWindow = qtw.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

我现在正尝试调用按钮单击以打印语句并退出应用程序,但我似乎无法正确执行此操作。有没有人能够帮助我并向我解释拨打此电话的正确程序应该是什么?

没有使用您的MainWindowclass;事实上你也在覆盖那个名字(还要注意变量名应该以大写字母开头,因为只有常量和classes 应该有大写的名字)。

class MainWindow(qtw.QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        # note: the button is a member of self.ui
        self.ui.play_button.clicked.connect(play_button_clicked)

    def play_button_clicked(self):
        print('woohoo this worked')
        # you should not directly use sys.exit from a widget
        qtw.QApplication.quit()

    
if __name__ == "__main__":
    import sys
    app = qtw.QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())

请注意,您甚至可以使用多重继承:

class MainWindow(qtw.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)
        # in this case, the button is a direct member of the class
        self.play_button.clicked.connect(play_button_clicked)

在关于 using Designer.

的官方 guidelines 中了解更多关于正确使用 ui 文件的信息