我正在努力为学校创造一些东西。我正在使用PyQT和QtDesigner。我不知道我不应该编辑PYUIC4生成的代码。有没有办法在打开新窗口时关闭窗口? 非常感谢任何帮助,谢谢:
from PyQt4 import QtCore, QtGui
from medLogin import *
import webbrowser
import time
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_titleMain(object):
class openUiMain(object):
def openLogin(self):
print("Opening Login...")
self.openLogin = QtGui.QMainWindow()
self.ui = Ui_loginMainWin()
self.ui.setupUi(self.openLogin)
self.openLogin.show()
def quitProgram(self):
print("Program terminating...")
time.sleep(0.5)
quit()
def openVleSite(self):
print("Opening VLE Revision page.")
time.sleep(0.5)
webbrowser.open("")
webbrowser.open("")
def setupUi(self, titleMain):
titleMain.setObjectName(_fromUtf8("titleMain"))
titleMain.resize(300, 470)
titleMain.setMinimumSize(QtCore.QSize(300, 470))
titleMain.setMaximumSize(QtCore.QSize(300, 470))
self.centralwidget = QtGui.QWidget(titleMain)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.mainTitle = QtGui.QLabel(self.centralwidget)
self.mainTitle.setGeometry(QtCore.QRect(0, 10, 301, 141))
self.mainTitle.setText(_fromUtf8(""))
self.mainTitle.setPixmap(QtGui.QPixmap(_fromUtf8("C:\Users\Elliot\Desktop\m\logo1.jpg")))
self.mainTitle.setAlignment(QtCore.Qt.AlignCenter)
self.mainTitle.setObjectName(_fromUtf8("mainTitle"))
self.bottomHorzLine = QtGui.QFrame(self.centralwidget)
self.bottomHorzLine.setGeometry(QtCore.QRect(0, 150, 301, 16))
self.bottomHorzLine.setFrameShape(QtGui.QFrame.HLine)
self.bottomHorzLine.setFrameShadow(QtGui.QFrame.Sunken)
self.bottomHorzLine.setObjectName(_fromUtf8("bottomHorzLine"))
self.topHorzLine = QtGui.QFrame(self.centralwidget)
self.topHorzLine.setGeometry(QtCore.QRect(0, 0, 301, 16))
self.topHorzLine.setFrameShape(QtGui.QFrame.HLine)
self.topHorzLine.setFrameShadow(QtGui.QFrame.Sunken)
self.topHorzLine.setObjectName(_fromUtf8("topHorzLine"))
self.beginButton = QtGui.QPushButton(self.centralwidget)
self.beginButton.setGeometry(QtCore.QRect(0, 330, 301, 28))
self.beginButton.setObjectName(_fromUtf8("beginButton"))
self.beginButton.clicked.connect(self.openLogin)
self.vleMedRevButton = QtGui.QPushButton(self.centralwidget)
self.vleMedRevButton.setGeometry(QtCore.QRect(0, 370, 301, 28))
self.vleMedRevButton.setObjectName(_fromUtf8("vleMedRevButton"))
self.vleMedRevButton.clicked.connect(self.openVleSite)
self.quitButton = QtGui.QPushButton(self.centralwidget)
self.quitButton.setGeometry(QtCore.QRect(0, 410, 301, 28))
self.quitButton.setObjectName(_fromUtf8("quitButton"))
self.quitButton.clicked.connect(self.quitProgram)
self.secondTitle = QtGui.QLabel(self.centralwidget)
self.secondTitle.setGeometry(QtCore.QRect(4, 160, 291, 151))
self.secondTitle.setObjectName(_fromUtf8("secondTitle"))
self.bottomHorzLine_2 = QtGui.QFrame(self.centralwidget)
self.bottomHorzLine_2.setGeometry(QtCore.QRect(0, 310, 301, 16))
self.bottomHorzLine_2.setFrameShape(QtGui.QFrame.HLine)
self.bottomHorzLine_2.setFrameShadow(QtGui.QFrame.Sunken)
self.bottomHorzLine_2.setObjectName(_fromUtf8("bottomHorzLine_2"))
titleMain.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(titleMain)
self.menubar.setGeometry(QtCore.QRect(0, 0, 300, 26))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuMenu = QtGui.QMenu(self.menubar)
self.menuMenu.setObjectName(_fromUtf8("menuMenu"))
titleMain.setMenuBar(self.menubar)
self.actionVisit_VLE_Page = QtGui.QAction(titleMain)
self.actionVisit_VLE_Page.setObjectName(_fromUtf8("actionVisit_VLE_Page"))
self.actionVisit_VLE_Page.triggered.connect(self.openVleSite)
self.actionQuit = QtGui.QAction(titleMain)
self.actionQuit.setObjectName(_fromUtf8("actionQuit"))
self.actionQuit.triggered.connect(self.quitProgram)
self.menuMenu.addAction(self.actionVisit_VLE_Page)
self.menuMenu.addAction(self.actionQuit)
self.menubar.addAction(self.menuMenu.menuAction())
self.retranslateUi(titleMain)
QtCore.QMetaObject.connectSlotsByName(titleMain)
def retranslateUi(self, titleMain):
titleMain.setWindowTitle(_translate("titleMain", "Revision Program", None))
self.beginButton.setText(_translate("titleMain", "Begin / Open menu", None))
self.vleMedRevButton.setText(_translate("titleMain", "Visit - Revision Page", None))
self.quitButton.setText(_translate("titleMain", "Quit Program", None))
self.secondTitle.setText(_translate("titleMain", "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:600;\"> Revision</span></p><p align=\"center\"><span style=\" font-size:12pt; font-weight:600;\">Program</span></p><p align=\"center\"><span style=\" font-size:10pt;\">Made with Python.<br/>Made to .</span></p><p align=\"center\"><span style=\" font-size:7pt; font-style:italic;\">Use the menu to choose your option.</span></p></body></html>", None))
self.menuMenu.setTitle(_translate("titleMain", "Menu", None))
self.actionVisit_VLE_Page.setText(_translate("titleMain", "Visit VLE Page", None))
self.actionQuit.setText(_translate("titleMain", "Quit", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
titleMain = QtGui.QMainWindow()
ui = Ui_titleMain.openUiMain()
ui.setupUi(titleMain)
titleMain.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
您的代码有点纠结,Qt Designer用于设计视图,建议不要修改它们,而是将它们用作另一个处理逻辑的类,这就是您所做的。如果您想显示基于QWidget
设计的视图的新窗口,则必须传递hide()
父级。
为了显示另一个窗口,必须将父项作为参数传递。因此,我们创建了对象,并使用show()
隐藏了上一个窗口,并使用def openLogin(self):
print("Opening Login...")
self.hide()
w = loginMainWin(self)
w.show()
显示新窗口。
pyuicx
另外作为建议:您不应修改Qt Designer
生成的文件,您必须从class TitleMain(QtGui.QMainWindow, openUiMain):
中使用的窗口继承该文件,并将设计作为参数传递。
from PyQt4 import QtCore, QtGui
from medLogin import *
import webbrowser
import time
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 openUiMain(object):
def setupUi(self, titleMain):
titleMain.setObjectName(_fromUtf8("titleMain"))
titleMain.resize(300, 470)
titleMain.setMinimumSize(QtCore.QSize(300, 470))
titleMain.setMaximumSize(QtCore.QSize(300, 470))
self.centralwidget = QtGui.QWidget(titleMain)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.mainTitle = QtGui.QLabel(self.centralwidget)
self.mainTitle.setGeometry(QtCore.QRect(0, 10, 301, 141))
self.mainTitle.setText(_fromUtf8(""))
self.mainTitle.setPixmap(QtGui.QPixmap("logo1.jpg"))
self.mainTitle.setAlignment(QtCore.Qt.AlignCenter)
self.mainTitle.setObjectName(_fromUtf8("mainTitle"))
self.bottomHorzLine = QtGui.QFrame(self.centralwidget)
self.bottomHorzLine.setGeometry(QtCore.QRect(0, 150, 301, 16))
self.bottomHorzLine.setFrameShape(QtGui.QFrame.HLine)
self.bottomHorzLine.setFrameShadow(QtGui.QFrame.Sunken)
self.bottomHorzLine.setObjectName(_fromUtf8("bottomHorzLine"))
self.topHorzLine = QtGui.QFrame(self.centralwidget)
self.topHorzLine.setGeometry(QtCore.QRect(0, 0, 301, 16))
self.topHorzLine.setFrameShape(QtGui.QFrame.HLine)
self.topHorzLine.setFrameShadow(QtGui.QFrame.Sunken)
self.topHorzLine.setObjectName(_fromUtf8("topHorzLine"))
self.beginButton = QtGui.QPushButton(self.centralwidget)
self.beginButton.setGeometry(QtCore.QRect(0, 330, 301, 28))
self.beginButton.setObjectName(_fromUtf8("beginButton"))
self.vleMedRevButton = QtGui.QPushButton(self.centralwidget)
self.vleMedRevButton.setGeometry(QtCore.QRect(0, 370, 301, 28))
self.vleMedRevButton.setObjectName(_fromUtf8("vleMedRevButton"))
self.quitButton = QtGui.QPushButton(self.centralwidget)
self.quitButton.setGeometry(QtCore.QRect(0, 410,301, 28))
self.quitButton.setObjectName(_fromUtf8("quitButton"))
self.secondTitle = QtGui.QLabel(self.centralwidget)
self.secondTitle.setGeometry(QtCore.QRect(4, 160, 291, 151))
self.secondTitle.setObjectName(_fromUtf8("secondTitle"))
self.bottomHorzLine_2 = QtGui.QFrame(self.centralwidget)
self.bottomHorzLine_2.setGeometry(QtCore.QRect(0, 310, 301, 16))
self.bottomHorzLine_2.setFrameShape(QtGui.QFrame.HLine)
self.bottomHorzLine_2.setFrameShadow(QtGui.QFrame.Sunken)
self.bottomHorzLine_2.setObjectName(_fromUtf8("bottomHorzLine_2"))
titleMain.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(titleMain)
self.menubar.setGeometry(QtCore.QRect(0, 0, 300, 26))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuMenu = QtGui.QMenu(self.menubar)
self.menuMenu.setObjectName(_fromUtf8("menuMenu"))
titleMain.setMenuBar(self.menubar)
self.actionVisit_VLE_Page = QtGui.QAction(titleMain)
self.actionVisit_VLE_Page.setObjectName(_fromUtf8("actionVisit_VLE_Page"))
self.actionVisit_VLE_Page.triggered.connect(self.openVleSite)
self.actionQuit = QtGui.QAction(titleMain)
self.actionQuit.setObjectName(_fromUtf8("actionQuit"))
self.actionQuit.triggered.connect(self.quitProgram)
self.menuMenu.addAction(self.actionVisit_VLE_Page)
self.menuMenu.addAction(self.actionQuit)
self.menubar.addAction(self.menuMenu.menuAction())
self.retranslateUi(titleMain)
QtCore.QMetaObject.connectSlotsByName(titleMain)
def retranslateUi(self, titleMain):
titleMain.setWindowTitle(_translate("titleMain", "Revision Program", None))
self.beginButton.setText(_translate("titleMain", "Begin / Open menu", None))
self.vleMedRevButton.setText(_translate("titleMain", "Visit - Revision Page", None))
self.quitButton.setText(_translate("titleMain", "Quit Program", None))
self.secondTitle.setText(_translate("titleMain", "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:600;\"> Revision</span></p><p align=\"center\"><span style=\" font-size:12pt; font-weight:600;\">Program</span></p><p align=\"center\"><span style=\" font-size:10pt;\">Made with Python.<br/>Made to .</span></p><p align=\"center\"><span style=\" font-size:7pt; font-style:italic;\">Use the menu to choose your option.</span></p></body></html>", None))
self.menuMenu.setTitle(_translate("titleMain", "Menu", None))
self.actionVisit_VLE_Page.setText(_translate("titleMain", "Visit VLE Page", None))
self.actionQuit.setText(_translate("titleMain", "Quit", None))
class TitleMain(QtGui.QMainWindow, openUiMain):
def __init__(self, parent=None):
super(TitleMain, self).__init__(parent=parent)
self.setupUi(self)
self.beginButton.clicked.connect(self.openLogin)
self.vleMedRevButton.clicked.connect(self.openVleSite)
self.quitButton.clicked.connect(self.quitProgram)
def openLogin(self):
print("Opening Login...")
self.hide()
w = loginMainWin(self)
w.show()
def quitProgram(self):
print("Program terminating...")
time.sleep(0.5)
self.close()
def openVleSite(self):
print("Opening VLE Revision page.")
time.sleep(0.5)
webbrowser.open("")
webbrowser.open("")
class loginMainWin(QtGui.QMainWindow, Ui_loginMainWin):
def __init__(self, parent=None):
super(loginMainWin, self).__init__(parent=parent)
self.setupUi(self)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
titleMain = TitleMain()
titleMain.show()
sys.exit(app.exec_())
完整代码:
x
xxx
xxxxx
xxxxxxx
xxxxxxxxx