简体   繁体   English

Pyqt 5 新窗口崩溃 Mainwindow

[英]Pyqt 5 new Window crashes Mainwindow

I have A MainWindow where i want to open a new Window.我有一个 MainWindow,我想在其中打开一个新窗口。

New Window:新窗口:

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

# Form implementation generated from reading ui file 'C:\Users\Tareq\Desktop\table.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(703, 449)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(0, 0, 701, 401))
        self.tableWidget.setRowCount(48)
        self.tableWidget.setColumnCount(4)
        self.tableWidget.setObjectName("tableWidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 703, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


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_())

I added to my MainWindow following Instructions我按照说明添加到我的主窗口

    self.pushButton_2.clicked.connect(self.create_new_window)

and the following function:以及以下功能:

def create_new_window(self):
    from tableWindow import Ui_MainWindow
    self.tableWindow = Ui_MainWindow
    self.tableWinow.show()

But after pressing the button2 the Programm crashes with no given Error...但是在按下 button2 后,程序崩溃了,没有给定的错误......

Process finished with exit code 1进程以退出代码 1 结束

As eyllanesc says it is probably because you have not imported QMainWindow before using it.正如 eyllanesc 所说,这可能是因为您在使用之前没有导入 QMainWindow。 However, even then it feels like you are doing too much to create your own window subclassing "object".但是,即便如此,您还是觉得您在创建自己的窗口子类化“对象”方面做得太多了。 It would be much easier (more Pythonic?) to subclass QMainWindow and let that do the heavy lifting.将 QMainWindow 子类化并让它完成繁重的工作会容易得多(更 Pythonic?)。

Try this little example:试试这个小例子:

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import *

class Second(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Second, self).__init__(parent)
        #Setting a title, locating and sizing the window
        self.title = 'My Second Window'
        self.left = 200
        self.top = 200
        self.width = 500
        self.height = 500
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.pushButton = QtWidgets.QPushButton("Close Me", self)
        self.pushButton.clicked.connect(self.on_pushButton_clicked)
        self.pushButton.move(120,120)

    def on_pushButton_clicked(self):
        self.close()

class First(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(First, self).__init__(parent)
        self.title = 'My First Window'
        self.left = 100
        self.top = 100
        self.width = 500
        self.height = 500
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.pushButton = QtWidgets.QPushButton("Open Me", self)
        self.pushButton.move(120,120)
        self.pushButton.clicked.connect(self.on_pushButton_clicked)      
        self.newWindow = Second(self)

    def on_pushButton_clicked(self):
        self.newWindow.show()

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

It just opens one window from another with a push button.它只是通过一个按钮从另一个窗口打开一个窗口。

PyQt5 window crashing without Error PyQt5 窗口崩溃没有错误

I also had the crashing main window without showing any error.我也有崩溃的主窗口,没有显示任何错误。 It is very hard to debug code without finding where errors are occurring in the code.如果不找出代码中发生错误的位置,就很难调试代码。 I recommend to use the following imports at the top of your code, you'll get error exceptions printed on the terminal我建议在代码顶部使用以下导入,您将在终端上打印错误异常

import cgitb
cgitb.enable(format = 'text')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM