我正在学习如何使用PyQt4,我似乎创建了一个简单的程序,当单击第一个窗口中的按钮时,该程序会打开第二个窗口。我的问题是,我希望第一个窗口关闭,因为第二个窗口打开。我在点击事件中使用了self.close()
函数无济于事。我需要帮助。
# -*- 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_())
答案 0 :(得分:1)
1)你在做一些时髦的事情:
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_())
如果我更改下面的代码,第一个窗口关闭:
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
#MainWindow.show()
ui.show()
sys.exit(app.exec_())
2)此代码:
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
调用setupUi()两次,因为创建一个Ui_MainWindow实例会导致调用setupUi():
class Ui_MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
...
self.setupUi(self)
3)你转换的.ui文件看起来很棘手:
class Ui_MainWindow(QtGui.QMainWindow):
我转换的.ui文件有类似的类(Qt 4.8.6),但该类继承自object - 而不是QtGui.QMainWindow,这意味着我的类不是widget类,它只是一个常规类,包含一堆数据和一些创建小部件的方法。你改变了吗?也许是为了回应错误:AttributeError: 'Ui_MainWindow' object has no attribute 'close'
?
我认为你不应该干涉转换后的.ui文件。相反,为您的代码创建一个不同的文件并导入转换后的.ui文件,即.py文件,您可以调用其中定义的方法。这允许您在Qt Designer中进行更改,然后再次将更改的.ui文件转换为.py文件 - 无需更改程序文件中的任何内容。并且,这样做会阻止你犯下你做的错误。
以下是一个简化示例,其中包含一个窗口,该窗口具有关闭窗口的按钮。您所做的相同错误将阻止此应用中的窗口关闭。但是将代码放在转换后的.ui文件的单独文件中,看看会发生什么:
design.py(对此转换的.ui文件没有更改):
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.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(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(568, 409)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(230, 150, 110, 32))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 568, 22))
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", "Close window", None))
main.py(为您的代码创建此文件):
import sys
import design
from PyQt4.QtGui import QMainWindow, QApplication
class MyWindow(QMainWindow, design.Ui_MainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.close_window)
def close_window(self):
self.close()
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
请注意,由于您只创建了一个窗口,因此无法显示错误的窗口。此外,如果您决定更改.ui文件(然后再将其转换为.py文件),唯一会改变的是内部函数,因为main.py只调用您赢得的功能无需对main.py进行任何更改。
因为MyWindow继承自design.py中定义的Ui_MainWindow类,所以使用self.widgetName在MyWindow类中可以访问该类中指定给self的所有小部件 - 当在MyWindow类中找不到widgetName时,搜索其父类的widgetName。