我在PyQt中创建了一个带有按钮的主窗口。我希望使用该按钮显示另一个窗口,并且主窗口应该关闭或隐藏。我编写了以下代码,但单击按钮后主窗口没有隐藏。
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
#Second window
class Ui_Dialog(QtGui.QWidget):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.setWindowModality(QtCore.Qt.WindowModal)
Dialog.resize(400, 300)
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
pass
#Main window
class Ui_MainWindow(QtGui.QWidget):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(309, 148)
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(50, 30, 191, 71))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Open Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.showDial)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
pass
#function to display another form
def showDial(self):
Dialog = QtGui.QDialog()
u = Ui_Dialog()
u.setupUi(Dialog)
self.close()
Dialog.exec_()
#self.hide()
#self.close()
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_())
sys.exit(app.exec_())
答案 0 :(得分:0)
麻烦在于showDial
,self是Ui对象,而不是窗口本身。就个人而言,我不喜欢以这种方式创建单独的对象(我假设这是受GUI设计者的启发),但您可以将对象本身存储在setupUi中并像这样引用它:
class Ui_MainWindow(QtGui.QWidget):
def setupUi(self, MainWindow):
MainWindow.setObjectName(("MainWindow"))
MainWindow.resize(309, 148)
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(50, 30, 191, 71))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Open Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setObjectName(("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.pushButton.clicked.connect(self.showDial)
self.win = MainWindow
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#function to display another form
def showDial(self):
Dialog = QtGui.QDialog()
u = Ui_Dialog()
u.setupUi(Dialog)
self.win.close()
Dialog.exec_()
#self.hide()
#self.close()
(另请注意,您两次致电app.exec_()
)
模式的简单替代方法:
MainWindow = Ui_MainWindow()
MainWindow.setupUi(MainWindow) #note that self and MainWindow are the same
让Ui_MainWindow继承QMainWindow而不是QWidget。