I am unable to open another Pyqt5 form

Asked

Viewed 655 times

0

I can’t seem to make the other window in my file appear.

Code:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'testando.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(950, 600)
        MainWindow.setMaximumSize(QtCore.QSize(950, 600))
        MainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.txtBemVindo = QtWidgets.QLabel(self.centralwidget)
        self.txtBemVindo.setGeometry(QtCore.QRect(270, 60, 431, 171))
        font = QtGui.QFont()
        font.setFamily("Narkisim")
        font.setPointSize(48)
        font.setUnderline(True)

        self.txtBemVindo.setFont(font)
        self.txtBemVindo.setObjectName("txtBemVindo")

        self.btInciar = QtWidgets.QPushButton(self.centralwidget)
        self.btInciar.setGeometry(QtCore.QRect(370, 300, 191, 81))
        self.btInciar.setObjectName("btInciar")
        self.btInciar.clicked.connect(self.evtIniciar)

        self.btSair = QtWidgets.QPushButton(self.centralwidget)
        self.btSair.setGeometry(QtCore.QRect(840, 500, 112, 34))
        self.btSair.setObjectName("btSair")
        self.btSair.clicked.connect(self.evtSair)
        MainWindow.setCentralWidget(self.centralwidget)

        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 950, 31))
        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", "MainWindow"))
        self.txtBemVindo.setText(_translate("MainWindow", "Bem Vindo"))
        self.btInciar.setText(_translate("MainWindow", "Clique Aqui Para Iniciar"))
        self.btSair.setText(_translate("MainWindow", "Sair"))

    def evtSair(self):
        exit(0)

    def evtIniciar(self):
        Tela1.show()


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


class Ui_Tela1(object):
    def setupUi(self, Tela1):
        Tela1.setObjectName("Tela1")
        Tela1.resize(600, 300)
        self.label = QtWidgets.QLabel(Tela1)
        self.label.setGeometry(QtCore.QRect(30, 50, 451, 161))
        font = QtGui.QFont()
        font.setPointSize(36)
        self.label.setFont(font)
        self.label.setObjectName("label")

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

    def retranslateUi(self, Tela1):
        _translate = QtCore.QCoreApplication.translate
        Tela1.setWindowTitle(_translate("Tela1", "Dialog"))
        self.label.setText(_translate("Tela1", "Bem Vindo"))


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    Tela1 = QtWidgets.QDialog()
    ui = Ui_Tela1()
    ui.setupUi(Tela1)
    Tela1.show()
    sys.exit(app.exec_())
  • 1

    Welcome to Stack Overflow. It would be good to build your question better by providing more details and explaining what is happening. Two-line "clarification" questions are difficult to understand and tend to get no answer.

1 answer

0

Your code is very disorganized. And there’s an example here if you want: https://stackoverflow.com/questions/36768033/pyqt-how-to-open-new-window

Try to use inheritance in the classes you use... And it’s interesting to use the "Generator UI code" just to have a base, then you copy only what you need for your project...
Your organized code:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Tela1(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Ui_Tela1, self).__init__(parent)
        self.setupUi()

    def setupUi(self):
        self.setObjectName("Tela1")
        self.resize(600, 300)
        self.label = QtWidgets.QLabel(self)
        self.label.setGeometry(QtCore.QRect(30, 50, 451, 161))
        font = QtGui.QFont()
        font.setPointSize(36)
        self.label.setFont(font)
        self.label.setObjectName("label")

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

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("Tela1", "Dialog"))
        self.label.setText(_translate("Tela1", "Bem Vindo"))


class Ui_MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__(parent)
        self.setupUi()
        self.tela1 = Ui_Tela1(self)

    def setupUi(self):
        self.setObjectName("MainWindow")
        self.resize(950, 600)
        self.setMaximumSize(QtCore.QSize(950, 600))
        self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))

        self.centralwidget = QtWidgets.QWidget(self)
        self.centralwidget.setObjectName("centralwidget")

        self.txtBemVindo = QtWidgets.QLabel(self.centralwidget)
        self.txtBemVindo.setGeometry(QtCore.QRect(270, 60, 431, 171))
        font = QtGui.QFont()
        font.setFamily("Narkisim")
        font.setPointSize(48)
        font.setUnderline(True)

        self.txtBemVindo.setFont(font)
        self.txtBemVindo.setObjectName("txtBemVindo")

        self.btInciar = QtWidgets.QPushButton(self.centralwidget)
        self.btInciar.setGeometry(QtCore.QRect(370, 300, 191, 81))
        self.btInciar.setObjectName("btInciar")
        self.btInciar.clicked.connect(self.evtIniciar)

        self.btSair = QtWidgets.QPushButton(self.centralwidget)
        self.btSair.setGeometry(QtCore.QRect(840, 500, 112, 34))
        self.btSair.setObjectName("btSair")
        self.btSair.clicked.connect(self.evtSair)
        self.setCentralWidget(self.centralwidget)

        self.menubar = QtWidgets.QMenuBar(self)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 950, 31))
        self.menubar.setObjectName("menubar")
        self.setMenuBar(self.menubar)

        self.statusbar = QtWidgets.QStatusBar(self)
        self.statusbar.setObjectName("statusbar")
        self.setStatusBar(self.statusbar)

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

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.txtBemVindo.setText(_translate("MainWindow", "Bem Vindo"))
        self.btInciar.setText(_translate("MainWindow", "Clique Aqui Para Iniciar"))
        self.btSair.setText(_translate("MainWindow", "Sair"))

    def evtSair(self):
        exit(0)

    def evtIniciar(self):
        self.tela1.show()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = Ui_MainWindow()
    MainWindow.show()
    sys.exit(app.exec_())

Browser other questions tagged

You are not signed in. Login or sign up in order to post.