如何在pyqt4中不同文件的QMainWindows之间切换?

时间:2018-09-09 08:52:39

标签: pyqt4

所以我有三个类login.pymainScreen.pyreportScreen.py。当某人成功登录后,我希望它转到mainScreen窗口并关闭login窗口(工作正常)。 问题出在下一部分,当它从mainScreen窗口切换到reportScreen窗口时,它显示reportScreen窗口,但没有隐藏上一个窗口,即mainScreen窗口因为win = QtGui.QMainWindow()是在if __name__=main部分中定义的。我不知道当从另一个文件中调用win = QtGui.QMainWindow()并关闭/隐藏mainScreen窗口时该如何访问。

请帮助我,切换到第三个窗口时如何关闭第二个窗口?

代码如下:

Login.py:

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from mainScreen import Ui_MainWindow2

class Ui_win(object):
  def openwindow(self):
    self.window = QtGui.QMainWindow()
    self.ui = Ui_MainWindow2()
    self.ui.setupUi(self.window)
    win.hide()
    self.window.show()

  def setupUi(self, win):
    win.setObjectName(_fromUtf8("win"))
    win.resize(622, 512)
    win.setMinimumSize(QtCore.QSize(622, 512))
    win.setMaximumSize(QtCore.QSize(622, 512))
    win.setAutoFillBackground(False)
    win.setStyleSheet(_fromUtf8("background-color: rgb(0, 39, 58);"))
    self.label = QtGui.QLabel(win)
    self.label.setGeometry(QtCore.QRect(30, 40, 401, 21))
    font = QtGui.QFont()
    font.setFamily(_fromUtf8("Noto Sans CJK SC"))
    font.setPointSize(20)
    font.setBold(True)
    font.setWeight(75)

    ...
import logo_rc

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    win = QtGui.QWidget()
    ui = Ui_win()
    ui.setupUi(win)
    win.show()
    db()
    sys.exit(app.exec_())

mainScreen.py:

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QMainWindow
from QtImageViewer import QtImageViewer
from reportScreen import Ui_MainWindow3
from PyQt4.QtGui import *

    def __init__(self):
    super(Ui_MainWindow2, self).__init__()
    self.win = QtGui.QMainWindow()
    self.imgFound = 'false'
    self.setupUi(self.win)

def openreport(self):
    if self.imgFound == 'true':
        self.window = QtGui.QMainWindow()
        self.ui = Ui_MainWindow3()
        self.ui.setupUi(self.window)
        self.window.show()

    else:
        QMessageBox.information(win, "No file chosen.", "Please choose file.")

def setupUi(self, MainWindow2):
    MainWindow2.setObjectName(_fromUtf8("MainWindow2"))
    MainWindow2.resize(759, 576)
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/img/logo.jpg")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    MainWindow2.setWindowIcon(icon)
    MainWindow2.setStyleSheet(_fromUtf8(""))
    self.centralwidget = QtGui.QWidget(MainWindow2)
    self.centralwidget.setStyleSheet(_fromUtf8("background-color: rgb(231, 231, 231);"))
    self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)

    ....

def retranslateUi(self, MainWindow2):
    MainWindow2.setWindowTitle(_translate("MainWindow2", "Welcome", None))
    self.choosefileBtn.setText(_translate("MainWindow2", "Choose File", None))
    self.predictBtn.setText(_translate("MainWindow2", "Predict", None))
    self.label.setText(_translate("MainWindow2", "              Alzheimer\'s Disease Detection", None))
    self.menuFile.setTitle(_translate("MainWindow2", "File", None))
    self.menuEdit.setTitle(_translate("MainWindow2", "Edit", None))
    self.menuView.setTitle(_translate("MainWindow2", "View", None))
    self.menuHelp.setTitle(_translate("MainWindow2", "Help", None))
    self.profile.setToolTip(_translate("MainWindow3", "sk", None))

def showimage(self):
    viewer = QtImageViewer()
    img = viewer.loadImageFromFile()
    self.imagefile.setPixmap(QtGui.QPixmap(_fromUtf8(img)))
    self.imgFound = 'true'


import logo_rc
import noimage_rc
import profileicon_rc

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    win = QtGui.QMainWindow()
    ui = Ui_MainWindow2()
    ui.setupUi(win)
    win.show()
    sys.exit(app.exec_())

reportScreen.py:

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *

class Ui_MainWindow3(object):
  def setupUi(self, MainWindow3):
    MainWindow3.setObjectName(_fromUtf8("MainWindow3"))
    MainWindow3.resize(759, 576)
    MainWindow3.setMaximumSize(QtCore.QSize(16777215, 16777215))
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/img/logo.jpg")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    MainWindow3.setWindowIcon(icon)
    MainWindow3.setStyleSheet(_fromUtf8(""))
    self.centralwidget = QtGui.QWidget(MainWindow3)
    self.centralwidget.setStyleSheet(_fromUtf8("background-color: rgb(231, 231, 231);"))
    self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
    self.gridLayoutWidget.setGeometry(QtCore.QRect(120, 110, 452, 452))
    self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
    self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
    self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
    self.reportimg = QtGui.QLabel(self.gridLayoutWidget)
    self.reportimg.setMaximumSize(QtCore.QSize(450, 450))
    self.reportimg.setFrameShape(QtGui.QFrame.NoFrame)
    self.reportimg.setText(_fromUtf8(""))
    self.reportimg.setPixmap(QtGui.QPixmap(_fromUtf8(":/report/Alzheimer\'s report.png")))
    self.reportimg.setScaledContents(True)

   ...

def retranslateUi(self, MainWindow3):
    MainWindow3.setWindowTitle(_translate("MainWindow3", "Welcome", None))
    self.label.setText(_translate("MainWindow3", "                       Alzheimer\'s Disease Detection", None))
    self.downloadicon.setToolTip(_translate("MainWindow3", "download", None))
    self.printicon.setToolTip(_translate("MainWindow3", "print", None))
    self.profile.setToolTip(_translate("MainWindow3", "sk", None))
    self.printicon_2.setToolTip(_translate("MainWindow3", "Go back", None))

def logout(self, event):
    QMessageBox.information(win3,"Logout", 'Click OK to logout.')
    sys.exit()

  import downloadicon_rc
  import goback_rc
  import logo_rc
  import noimage_rc
  import printicon_rc
  import profileicon_rc
  import report_rc

  if __name__ == "__main__":
      import sys
      app = QtGui.QApplication(sys.argv)
      win3 = QtGui.QMainWindow()
      ui = Ui_MainWindow3()
      ui.setupUi(win3)
      win3.show()
      sys.exit(app.exec_())

0 个答案:

没有答案