我正在學習如何使用PyQt4,我似乎已經創建了一個簡單的程序,當第一個窗口中的按鈕被點擊時,打開第二個窗口。我的問題是,我希望在第二個窗口打開時關閉第一個窗口。我在click事件中使用了self.close()
函數無濟於事。我需要幫助。PyQt窗口不關閉
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'uitest.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setupUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.setWindowModality(QtCore.Qt.NonModal)
MainWindow.resize(288, 242)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(50, 50, 171, 101))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
####################################################################################
self.pushButton.clicked.connect(self.ChangeUi)
########################################################################
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 288, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "NEXT", None))
def ChangeUi(self):
self.newUser = Ui_MainWindow1()
self.newUser.show()
class Ui_MainWindow1(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setupUi(self)
def setupUi(self, MainWindow1):
MainWindow1.setObjectName(_fromUtf8("MainWindow1"))
MainWindow1.resize(480, 265)
self.centralwidget = QtGui.QWidget(MainWindow1)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(180, 70, 111, 71))
font = QtGui.QFont()
font.setPointSize(15)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
MainWindow1.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow1)
self.menubar.setGeometry(QtCore.QRect(0, 0, 480, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow1.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow1)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow1.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow1)
QtCore.QMetaObject.connectSlotsByName(MainWindow1)
def retranslateUi(self, MainWindow1):
MainWindow1.setWindowTitle(_translate("MainWindow1", "MainWindow", None))
self.label.setText(_translate("MainWindow1", "IT WORKED", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我希望你不要介意,我編輯了你的帖子,在python標籤旁邊添加一個'python'標籤,這樣所有的代碼都會被python語法高亮顯示,代碼更易於閱讀。 – 7stud