编辑:例如,由于上述原因,这段代码可以正常运行——您必须在函数退出之前单击按钮
这个问题已经困扰我好几天了。我试图从主窗口打开一个Ui_form。由于某些原因,子窗口立即打开并关闭。
class Ui_MainWindow(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QMainWindow.__init__(self,parent)
def setupUi(self, MainWindow):
# Some Gui Code
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(406, 234)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.listView = QtGui.QListView(self.centralwidget)
self.listView.setObjectName(_fromUtf8("listView"))
self.horizontalLayout.addWidget(self.listView)
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.horizontalLayout.addWidget(self.pushButton)
self.verticalLayout.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 406, 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.QObject.connect(self.pushButton , QtCore.SIGNAL("clicked()") , self.NewLog)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "New Log", None, QtGui.QApplication.UnicodeUTF8))
def NewLog(self):
app = QtGui.QWidget()
myapp = new_application_log.Ui_Form()
myapp.setupUi(app)
myapp.show()
#app.exec_()
class Ui_Form(QtGui.QWidget):
def __init__(self,parent = None):
QtGui.QWidget.__init__(self,parent)
def setupUi(self, Form):
# Some Gui Code.
Form.setObjectName(_fromUtf8("Form"))
Form.resize(312, 269)
self.verticalLayout = QtGui.QVBoxLayout(Form)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.listView = QtGui.QListView(Form)
self.listView.setObjectName(_fromUtf8("listView"))
self.verticalLayout.addWidget(self.listView)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
def main():
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow() # <-- Instantiate QMainWindow object.
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
app.exec_()
最简单的解决方案是将newLog更改为
def NewLog(self):
app = QtGui.QWidget()
self.form = Ui_Form()
self.form.setupUi(app)
self.form.show()
与Ui_mainwindow
类中定义的self.form
class Ui_MainWindow(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QMainWindow.__init__(self,parent)
self.form = None
在您的代码中,您在newLog
函数内部定义myapp = Ui_form()
变量,然后它将超出作用域并在函数退出时被删除。如果要以某种方式包含一些函数结束的阻塞,则技术上不需要将self.form
放在Ui_mainWindow
的__init__()
中。例如,您有一个表单,它将返回需要点击按钮才能关闭,然后您可以按照您所拥有的那样做,但由于您只是show()
一个小部件,您需要让它在函数之外存活。
编辑:例如,由于上述原因,这段代码可以正常运行——您必须在函数退出之前单击按钮
def NewLog(self, text='I am open!', buttons=QtGui.QMessageBox.Ok):
errorBox = QtGui.QMessageBox()
errorBox.setText(text)
errorBox.setStandardButtons(buttons)
return errorBox.exec_()