TransWikia.com

Não estou conseguindo abrir outro Form do PyQt5

Stack Overflow em Português Asked by Nadson Alexandre on November 22, 2021

Não estou conseguindo fazer com que a outra janela do meu arquivo apareça.

Código:

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

One Answer

Seu código está bem desorganizado. E tem um exemplo aqui se quiser: https://stackoverflow.com/questions/36768033/pyqt-how-to-open-new-window

Tente usar herança nas classes que usar... E é interessante usar o "UI code generator" apenas para ter uma base, depois você copia somente o necessário para seu projeto...
Seu código organizado:

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

Answered by Jeff on November 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP