2017-09-17 432 views
0

我有一個MainWindow,我想打開一個新窗口。Pyqt 5新窗口崩潰主窗口

新窗口:

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

我加入到我的主窗口下面的說明

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後PROGRAMM沒有崩潰給定錯誤...

過程完成,退出代碼爲1

+0

什麼是'MainWindow'? – eyllanesc

+0

我的主窗口PY文件如果我有一個按鈕@eyllanesc –

+0

嘗試:'高清create_new_window(個體經營):從tableWindow進口Ui_MainWindow UI = Ui_MainWindow() self.tableWindow =的QMainWindow()ui.setupUi(self.tableWindow)自 .tableWindow.show()' – eyllanesc

回答

0

由於eyllanesc說,這可能是因爲你還沒有使用它之前進口的QMainWindow。然而,即使這樣,你覺得你正在做太多的事來創建你自己的窗口子類化「對象」。這將是更容易(更Pythonic?)子類化QMainWindow,並讓它做繁重的工作。

試試這個小例子:

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

它只是打開從另一個與一個按鈕一個窗口。