我是python的新手。我正在使用pyqt5进行GUI开发。我有一个主窗口,应关闭该窗口,并在单击按钮后出现新对话框。但是它不会关闭,也不会显示任何错误并打开nextDialog。我还想在nextDialog中单击“确定”按钮时关闭nextDialog。请帮助检查问题。我正在尝试开发一个新项目,但在这个问题上陷于困境。代码如下。
from PyQt5 import QtCore, QtGui, QtWidgets
from nextDialog import Ui_Dialog
class Ui_MainWindow(QtWidgets.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(370, 171)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 50, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.pushButton.clicked.connect(self.opennext)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def opennext(self):
self.Dialog = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.Dialog)
self.Dialog.show()
self.close() #Not working
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Open"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_Dialog(QtWidgets.QMainWindow):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(346, 182)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(110, 80, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.exit)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def exit(self):
self.hide() #This also not working, I want either of these two to
#work
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "OK"))
答案 0 :(得分:0)
尝试一下:
main.py
y = 0
def foo():
return y # This is a local variable (but unbound so will error)
y = 1 # this is unreachable but still makes `y` local.
nextDialog.py
from PyQt5 import QtCore, QtGui, QtWidgets
from nextDialog import Ui_Dialog
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.centralwidget = QtWidgets.QWidget()
self.setCentralWidget(self.centralwidget)
self.pushButton = QtWidgets.QPushButton("Open", self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 50, 75, 23))
self.pushButton.clicked.connect(self.opennext)
def opennext(self):
self.Dialog = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.Dialog)
self.Dialog.show()
self.close() #Not working
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
# ui = Ui_MainWindow()
# ui.setupUi(MainWindow)
window.show()
sys.exit(app.exec_())
答案 1 :(得分:0)
我有点难以理解您的代码,我只是使用Designer来制作我的GUI,
无论如何,我为您的main.py做了拼图的第一部分
第4行应显示为class Ui_MainWindow(object):
并关闭第25车道的第一个窗口,请使用MainWindow.close()
在一开始也添加了import sys
,这里是代码
main.py:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 18 17:43:19 2020
@author: Pietro
"""
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from nextDialog import Ui_Dialog
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(370, 171)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 50, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.pushButton.clicked.connect(self.opennext)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def opennext(self):
self.Dialog = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.Dialog)
self.Dialog.show()
MainWindow.close()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Open"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
第一个打开的窗口,然后在按下打开的按钮并出现新窗口时将其关闭,谜语的第二部分将花费更长的时间,我会在有空余时间的情况下尽快尝试
下面答案中的Dialog.py似乎不知道为什么起作用(也不知道main.py的工作原理,但是对我来说,def退出不起作用的事实更不合逻辑)。
nextDialog.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 18 17:43:19 2020
@author: Pietro
"""
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(QtWidgets.QMainWindow):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(346, 182)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(110, 80, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(Dialog.close) # as answer below dont know why def exit doesnt work
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
# def exit(self):
# print('exit' *5)
# Dialog.close()
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "OK"))