简体   繁体   中英

Pyqt 5 new Window crashes Mainwindow

I have A MainWindow where i want to open a new Window.

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...

Process finished with exit code 1

As eyllanesc says it is probably because you have not imported QMainWindow before using it. 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.

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

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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