How to open a second window in PyQt5 without closing the main one?
I am creating a login interface, and I want that when the user presses a button, it opens a new window in PyQt5 but without closing the main window, but the secondary window is not getting 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()
0
2 answers
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 I 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
Author: CAMTools, 2020-09-01 19:02:29
I will make a small update to my code. Having us in notes.comb a combobox let's get some option of this combobox and then "take" this option to a textBrowser in MainWindow.{
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())
-3
Author: CAMTools, 2020-09-01 20:02:06