Pyqt 5新窗口崩溃主窗口

时间:2017-09-17 22:37:51

标签: python pandas pyqt pyqt5

我有一个MainWindow,我想打开一个新窗口。

新窗口:

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

我按照说明

添加到我的MainWindow
    self.pushButton_2.clicked.connect(self.create_new_window)

以及以下功能:

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

但是在按下按钮2后,程序崩溃而没有给出错误...

  

使用退出代码1完成处理

2 个答案:

答案 0 :(得分:0)

正如eyllanesc所说,可能是因为你在使用之前没有导入QMainWindow。然而,即便如此,感觉你做了太多的事情来创建自己的窗口子类化" object"。将QMainWindow子类化为更加容易(更多Pythonic?)并让它重做起来。

试试这个小例子:

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

只需用按钮打开另一个窗口。

答案 1 :(得分:0)

PyQt5 窗口崩溃没有错误

我也有崩溃的主窗口,但没有显示任何错误。如果不找出代码中发生错误的位置,就很难调试代码。 我建议在代码顶部使用以下导入,您将在终端上打印错误异常

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