【问题标题】:Inheriting PyQt5 class继承 PyQt5 类
【发布时间】:2015-07-16 12:44:10
【问题描述】:

我正在尝试关注YouTube tutorials,了解如何创建 GUI 并创建一个使用 PyQt 继承 GUI 的类。这些教程使用 PyQt4,而我有 PyQt5。我做了一些更改以不出现引用错误,但窗口没有显示。

我的班级:

import sys
from ETF.modules.profiles_input.gui import *

class ProfileInput(QtWidgets.QMainWindow):

    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.ui = Ui_ProfileInputWindow()
        self.ui.setupUi(self)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    app = ProfileInput()
    app.show()
    sys.exit(app.exec_())

我的 GUI 课程以: (这段代码没问题,我把它包括在内仅供参考)

    from PyQt5 import QtCore, QtGui, QtWidgets

    class Ui_ProfileInputWindow(object):
        def setupUi(self, ProfileInputWindow):
            ProfileInputWindow.setObjectName("ProfileInputWindow")
            ProfileInputWindow.resize(880, 495)
            self.centralwidget = QtWidgets.QWidget(ProfileInputWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
            self.horizontalLayout.setObjectName("horizontalLayout")
            self.verticalLayout_2 = QtWidgets.QVBoxLayout()
            self.verticalLayout_2.setObjectName("verticalLayout_2")
            self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
            self.tabWidget.setObjectName("tabWidget")
            self.tab = QtWidgets.QWidget()
            self.tab.setObjectName("tab")
            self.verticalLayout = QtWidgets.QVBoxLayout(self.tab)
            self.verticalLayout.setObjectName("verticalLayout")
            self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
            self.horizontalLayout_2.setObjectName("horizontalLayout_2")
            self.lineEdit = QtWidgets.QLineEdit(self.tab)
            self.lineEdit.setObjectName("lineEdit")
            self.horizontalLayout_2.addWidget(self.lineEdit)
            self.pushButton = QtWidgets.QPushButton(self.tab)
            self.pushButton.setObjectName("pushButton")
            self.horizontalLayout_2.addWidget(self.pushButton)
            self.verticalLayout.addLayout(self.horizontalLayout_2)
            self.tableView = QtWidgets.QTableView(self.tab)
            self.tableView.setObjectName("tableView")
            self.verticalLayout.addWidget(self.tableView)
            self.tabWidget.addTab(self.tab, "")
            self.tab_2 = QtWidgets.QWidget()
            self.tab_2.setObjectName("tab_2")
            self.tabWidget.addTab(self.tab_2, "")
            self.verticalLayout_2.addWidget(self.tabWidget)
            self.horizontalLayout.addLayout(self.verticalLayout_2)
            ProfileInputWindow.setCentralWidget(self.centralwidget)

            self.retranslateUi(ProfileInputWindow)
            self.tabWidget.setCurrentIndex(0)
            self.pushButton.clicked.connect(self.pushButton.click)
            QtCore.QMetaObject.connectSlotsByName(ProfileInputWindow)

当我执行第一段代码时,python 解释器刚刚关闭并显示以下消息:

Process finished with exit code -1073741819 (0xC0000005)

第一段代码有什么问题?

提前致谢。

【问题讨论】:

  • 不是你设置app两次的问题吗?它应该包含对应用程序的引用,而不是对主窗口的引用。把它放在一个单独的变量中。

标签: python-3.x pyqt5


【解决方案1】:

您在这里分配给app 两次:

app = QtWidgets.QApplication(sys.argv)
app = ProfileInput()
app.show()
sys.exit(app.exec_())

代码应如下所示:

app = QtWidgets.QApplication(sys.argv)
window = ProfileInput()
window.show()
sys.exit(app.exec_())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-15
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2012-07-12
    相关资源
    最近更新 更多