How to open a second window in Pyqt5 without closing the main?

Asked

Viewed 335 times

1

I am creating a login interface, and I want when the user presses a button, to open a new window in Pyqt5 but without closing the main window, but the secondary window is not staying open.

from PyQt5 import uic, QtWidgets

def ButtonClick():
    formSec = uic.loadUi("JanSec.ui")
    formSec.show()

app = QtWidgets.QApplication([])
form = uic.loadUi("jan.ui")

form.Button.clicked.connect(ButtonClick)

form.show()
app.exec()

2 answers

-3

Hello, here is a complete script that I use in my programs. Surely you should adapt it to your case. It has an extra function called center that serves to center the main window on the screen. I hope to have helped.

from PyQt5 import uic
from PyQt5.QtWidgets import (
                        QMainWindow,
                        QApplication,
                        QDesktopWidget
                        )

form_2, base_2 = uic.loadUiType('notes.ui')


class MainNotes(base_2, form_2):
    def __init__(self, parent=None):
    super(base_2, self).__init__(parent)
    self.setupUi(self)


class MainApp(QMainWindow):
    """ Main Class
    """
    def __init__(self):
        super(MainApp, self).__init__()
        self.mainnotes = MainNotes()
        self.ui = uic.loadUi('MainWindow.ui', self)

        self.initapp()

    def initapp(self):
        self.ui.bt_notes.clicked.connect(self.notes)

    def notes(self):
       """
       Put your code here
       """
       self.mainnotes.show()

    def center(self):
       qr = self.frameGeometry()
       cp = QDesktopWidget().availableGeometry().center()
       qr.moveCenter(cp)
       self.move(qr.topLeft())


def main():
    import sys
try:
    myapp = QApplication([])
    mywindow = MainApp()
    mywindow.center()
    mywindow.show()
    myapp.exec_()
except SystemExit:
    sys.exit(0)


if __name__ == '__main__':
main()

-3

I’m gonna do a little update on my code. Having us in Notes.ui a combobox we will get some option of this combobox and then "take" this option up to a textbrowser in Mainwindow.ui

datacbox = ([" ", "linha 1", "linha 2", "linha 3", "etc"])

def initapp(self):
    self.ui.bt_notes.clicked.connect(self.notes)
    self.mainnotes.comboBox.currentTextChanged.connect(self.combodown)

def notes(self):
    """
    Put your code here
    """
    self.mainnotes.comboBox.addItems(datacbox)
    self.mainnotes.show()

 def combodown(self):
    self.ui.textBrowser_MAG2.setText(self.mainnotes.comboBox.currentText())

Browser other questions tagged

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