gpt4 book ai didi

python - 从 PyQt 的主窗口打开 Ui_Form

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:24 34 4
gpt4 key购买 nike

这个问题困扰了我好几天了。我正在尝试从主窗口打开 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_()

关于python - 从 PyQt 的主窗口打开 Ui_Form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23661106/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com