简体   繁体   中英

Pyqt5 Qt Designer Problem with Back button

I'm new and i need help. I what to have a function back bottun for return to the main page. I going to include the code of the two windows so please give me a hand.

Main.py

import sys    
from PyQt5 import QtCore, QtGui, QtWidgets
from Registrati import Ui_RegistratiWindows

class Ui_MainWindow(object):
        def openWindow(self):
            self.window = QtWidgets.QMainWindow()
            self.ui = Ui_RegistratiWindows()
            self.ui.setupUi(self.window)
            MainWindow.hide()
            self.window.show()
def showLabel(self):
    self.labeltestohelp.show()
    self.labeltestohelp1.show()

def setupUi(self, MainWindow):
    self.window = QtWidgets.QMainWindow()
    self.ui = Ui_RegistratiWindows()
    self.ui.setupUi(self.window)
    self.window.destroy()
    MainWindow.setObjectName("MainWindow")
    MainWindow.setWindowModality(QtCore.Qt.WindowModal)
    MainWindow.resize(222, 585)
    MainWindow.setMinimumSize(QtCore.QSize(222, 585))
    MainWindow.setMaximumSize(QtCore.QSize(222, 585))
    MainWindow.setBaseSize(QtCore.QSize(0, 0))
    MainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
    MainWindow.setFocusPolicy(QtCore.Qt.TabFocus)
    MainWindow.setStatusTip("")
    MainWindow.setWhatsThis("")
    MainWindow.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
    MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.LoginBotton = QtWidgets.QPushButton(self.centralwidget)
    self.LoginBotton.setGeometry(QtCore.QRect(10, 160, 201, 51))
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap("loginicon-png.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
    self.LoginBotton.setIcon(icon)
    self.LoginBotton.setIconSize(QtCore.QSize(24, 24))
    self.LoginBotton.setAutoDefault(False)
    self.LoginBotton.setObjectName("LoginBotton")
    self.RegistratiBotton = QtWidgets.QPushButton(self.centralwidget)
    self.RegistratiBotton.setGeometry(QtCore.QRect(10, 280, 201, 51))
    icon1 = QtGui.QIcon()
    icon1.addPixmap(QtGui.QPixmap("RegisterIcon.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
    self.RegistratiBotton.setIcon(icon1)
    self.RegistratiBotton.setIconSize(QtCore.QSize(24, 24))
    self.RegistratiBotton.setObjectName("RegistratiBotton")

    self.RegistratiBotton.clicked.connect(self.openWindow)

    self.Esci = QtWidgets.QPushButton(self.centralwidget)
    self.Esci.setGeometry(QtCore.QRect(160, 510, 61, 31))
    icon2 = QtGui.QIcon()
    icon2.addPixmap(QtGui.QPixmap("exit-icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
    self.Esci.setIcon(icon2)
    self.Esci.setObjectName("Esci")
    self.labelImag = QtWidgets.QLabel(self.centralwidget)
    self.labelImag.setGeometry(QtCore.QRect(0, 10, 221, 121))
    self.labelImag.setText("")
    self.labelImag.setPixmap(QtGui.QPixmap("TopImage.png"))
    self.labelImag.setScaledContents(True)
    self.labelImag.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
    self.labelImag.setWordWrap(False)
    self.labelImag.setObjectName("labelImag")
    self.InfoButton = QtWidgets.QPushButton(self.centralwidget)
    self.InfoButton.setGeometry(QtCore.QRect(160, 390, 61, 51))
    icon3 = QtGui.QIcon()
    icon3.addPixmap(QtGui.QPixmap("Info.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
    self.InfoButton.setIcon(icon3)
    self.InfoButton.setObjectName("InfoButton")

    self.InfoButton.clicked.connect(self.showLabel)

    self.labeltestohelp = QtWidgets.QLabel(self.centralwidget)
    self.labeltestohelp.setEnabled(False)
    self.labeltestohelp.setGeometry(QtCore.QRect(10, 110, 201, 41))
    self.labeltestohelp.setCursor(QtGui.QCursor(QtCore.Qt.WhatsThisCursor))
    self.labeltestohelp.setFrameShape(QtWidgets.QFrame.Box)
    self.labeltestohelp.setWordWrap(True)
    self.labeltestohelp.setIndent(10)
    self.labeltestohelp.setObjectName("labeltestohelp")
    self.labeltestohelp.hide()
    self.labeltestohelp1 = QtWidgets.QLabel(self.centralwidget)
    self.labeltestohelp1.setEnabled(False)
    self.labeltestohelp1.setGeometry(QtCore.QRect(10, 230, 201, 41))
    self.labeltestohelp1.setCursor(QtGui.QCursor(QtCore.Qt.WhatsThisCursor))
    self.labeltestohelp1.setMouseTracking(False)
    self.labeltestohelp1.setFrameShape(QtWidgets.QFrame.Box)
    self.labeltestohelp1.setTextFormat(QtCore.Qt.AutoText)
    self.labeltestohelp1.setWordWrap(True)
    self.labeltestohelp1.setIndent(18)
    self.labeltestohelp1.setOpenExternalLinks(False)
    self.labeltestohelp1.setObjectName("labeltestohelp1")
    self.labeltestohelp1.hide()
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 222, 21))
    self.menubar.setObjectName("menubar")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "E-Learning"))
    self.LoginBotton.setText(_translate("MainWindow", "Login"))
    self.RegistratiBotton.setText(_translate("MainWindow", "Registrati"))
    self.Esci.setText(_translate("MainWindow", "Esci"))
    self.InfoButton.setText(_translate("MainWindow", "Info"))
    self.labeltestohelp.setText(_translate("MainWindow", "Info: Usa Login se sei già Registrato!"))
    self.labeltestohelp1.setText(_translate("MainWindow", "Info: Registrati per Accedere ai contenuti"))



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_())

and the second windows Registrati.py

import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from Domanda import Ui_Infowindows


class Ui_RegistratiWindows(object):
    def Indietro(self):
        os.system("py Main.py")

    def showLabel(self):
        self.labeltestohelp.show()
        self.labeltestohelp1.show()
        self.labeltestohelp1_3.show()
    def setupUi(self, RegistratiWindows):
        RegistratiWindows.setObjectName("RegistratiWindows")
        RegistratiWindows.resize(222, 585)
        RegistratiWindows.setMinimumSize(QtCore.QSize(222, 585))
        RegistratiWindows.setMaximumSize(QtCore.QSize(222, 585))
        self.centralwidget = QtWidgets.QWidget(RegistratiWindows)
        self.centralwidget.setObjectName("centralwidget")
        self.labelImag = QtWidgets.QLabel(self.centralwidget)
        self.labelImag.setGeometry(QtCore.QRect(60, 50, 111, 101))
        self.labelImag.setText("")
        self.labelImag.setPixmap(QtGui.QPixmap("img_504963.png"))
        self.labelImag.setScaledContents(True)
        self.labelImag.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.labelImag.setWordWrap(False)
        self.labelImag.setObjectName("labelImag")
        self.labelTitolo = QtWidgets.QLabel(self.centralwidget)
        self.labelTitolo.setGeometry(QtCore.QRect(0, 0, 221, 41))
        font = QtGui.QFont()
        font.setFamily("Gill Sans MT")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.labelTitolo.setFont(font)
        self.labelTitolo.setScaledContents(False)
        self.labelTitolo.setAlignment(QtCore.Qt.AlignCenter)
        self.labelTitolo.setObjectName("labelTitolo")
        self.lineEditUsername = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEditUsername.setGeometry(QtCore.QRect(12, 190, 201, 20))
        self.lineEditUsername.setObjectName("lineEditUsername")
        self.lineEditUsername_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEditUsername_2.setGeometry(QtCore.QRect(10, 290, 201, 20))
        self.lineEditUsername_2.setObjectName("lineEditUsername_2")
        self.labelusername = QtWidgets.QLabel(self.centralwidget)
        self.labelusername.setGeometry(QtCore.QRect(10, 170, 101, 16))
        self.labelusername.setObjectName("labelusername")
        self.labelusername_2 = QtWidgets.QLabel(self.centralwidget)
        self.labelusername_2.setGeometry(QtCore.QRect(10, 270, 101, 16))
        self.labelusername_2.setObjectName("labelusername_2")
        self.InfoButton = QtWidgets.QPushButton(self.centralwidget)
        self.InfoButton.setGeometry(QtCore.QRect(160, 390, 61, 51))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("Info.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
        self.InfoButton.setIcon(icon)
        self.InfoButton.setObjectName("InfoButton")

        self.InfoButton.clicked.connect(self.showLabel)

        self.Esci_2 = QtWidgets.QPushButton(self.centralwidget)
        self.Esci_2.setGeometry(QtCore.QRect(160, 510, 61, 31))
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("exit-icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
        self.Esci_2.setIcon(icon1)
        self.Esci_2.setObjectName("Esci_2")
        self.labeltestohelp = QtWidgets.QLabel(self.centralwidget)
        self.labeltestohelp.setEnabled(False)
        self.labeltestohelp.setGeometry(QtCore.QRect(10, 220, 201, 41))
        self.labeltestohelp.setCursor(QtGui.QCursor(QtCore.Qt.WhatsThisCursor))
        self.labeltestohelp.setFrameShape(QtWidgets.QFrame.Box)
        self.labeltestohelp.setWordWrap(True)
        self.labeltestohelp.setIndent(15)
        self.labeltestohelp.setObjectName("labeltestohelp")
        self.labeltestohelp.hide()
        self.labeltestohelp1 = QtWidgets.QLabel(self.centralwidget)
        self.labeltestohelp1.setEnabled(False)
        self.labeltestohelp1.setGeometry(QtCore.QRect(10, 320, 201, 41))
        self.labeltestohelp1.setCursor(QtGui.QCursor(QtCore.Qt.WhatsThisCursor))
        self.labeltestohelp1.setMouseTracking(False)
        self.labeltestohelp1.setFrameShape(QtWidgets.QFrame.Box)
        self.labeltestohelp1.setTextFormat(QtCore.Qt.AutoText)
        self.labeltestohelp1.setWordWrap(True)
        self.labeltestohelp1.setIndent(18)
        self.labeltestohelp1.setOpenExternalLinks(False)
        self.labeltestohelp1.setObjectName("labeltestohelp1")
        self.labeltestohelp1.hide()
        self.labeltestohelp1_3 = QtWidgets.QLabel(self.centralwidget)
        self.labeltestohelp1_3.setEnabled(False)
        self.labeltestohelp1_3.setGeometry(QtCore.QRect(70, 390, 81, 81))
        self.labeltestohelp1_3.setCursor(QtGui.QCursor(QtCore.Qt.WhatsThisCursor))
        self.labeltestohelp1_3.setMouseTracking(False)
        self.labeltestohelp1_3.setFrameShape(QtWidgets.QFrame.Box)
        self.labeltestohelp1_3.setTextFormat(QtCore.Qt.AutoText)
        self.labeltestohelp1_3.setWordWrap(True)
        self.labeltestohelp1_3.setIndent(8)
        self.labeltestohelp1_3.setOpenExternalLinks(False)
        self.labeltestohelp1_3.setObjectName("labeltestohelp1_3")
        self.labeltestohelp1_3.hide()
        self.RegButtun = QtWidgets.QPushButton(self.centralwidget)
        self.RegButtun.setGeometry(QtCore.QRect(0, 390, 61, 61))
        self.RegButtun.setText("Registrati")
        self.RegButtun.setObjectName("RegButtun")
        self.IndietroButtun = QtWidgets.QPushButton(self.centralwidget)
        self.IndietroButtun.setGeometry(QtCore.QRect(0, 480, 61, 61))
        self.IndietroButtun.setText("Back")
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap("back.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
        self.IndietroButtun.setIcon(icon2)
        self.IndietroButtun.setObjectName("IndietroButtun")

        self.IndietroButtun.clicked.connect(self.Indietro)

        RegistratiWindows.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(RegistratiWindows)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 222, 21))
        self.menubar.setObjectName("menubar")
        RegistratiWindows.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(RegistratiWindows)
        self.statusbar.setObjectName("statusbar")
        RegistratiWindows.setStatusBar(self.statusbar)

        self.retranslateUi(RegistratiWindows)
        QtCore.QMetaObject.connectSlotsByName(RegistratiWindows)

    def retranslateUi(self, RegistratiWindows):
        _translate = QtCore.QCoreApplication.translate
        RegistratiWindows.setWindowTitle(_translate("RegistratiWindows", "Registrati"))
        self.labelTitolo.setText(_translate("RegistratiWindows", "Registra il tuo volto"))
        self.labelusername.setText(_translate("RegistratiWindows", "Username:"))
        self.labelusername_2.setText(_translate("RegistratiWindows", "Password:"))
        self.InfoButton.setText(_translate("RegistratiWindows", "Info"))
        self.Esci_2.setText(_translate("RegistratiWindows", "Esci"))
        self.labeltestohelp.setText(_translate("RegistratiWindows", "Info: Inserisci il tuo Username che verra usato per identificarti"))
        self.labeltestohelp1.setText(_translate("RegistratiWindows", "Info: Inserisci la tua Password per accedere ai tuoi contenuti"))
        self.labeltestohelp1_3.setText(_translate("RegistratiWindows", "Info: Clicca su Registrati per far iniziare la fase di registrazione del tuo volto"))




if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    RegistratiWindows = QtWidgets.QMainWindow()
    ui = Ui_RegistratiWindows()
    ui.setupUi(RegistratiWindows)
    RegistratiWindows.show()
    sys.exit(app.exec_())

So whit this two windows i can manage to open from Main.py->Registrati.py but when i want to return to the main windows i can use os.system() (brutaly but works) but the Registrati windows remains open in the background. So pleas gime me same tips for colsing the Registrati windows. Thanks!.

Depending on what you want to do (do you want to run two separate applications or Registrati.py is simply a dialog to be open by Main.py ?) there are two solutions:

  1. the first solution is to make Registrati.py a dialog so from the Main.py you don't run a complete application and your Back button is nothing more than the Close button of the dialog.
  2. the second solution is to connect the Back button to the quit() slot of the appplication launched by Registrati.py , this way you close the second application and have only the firse one still running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM