Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cant keep a second window open when pressing on a button



  • class MainWindow(QMainWindow):
    switch_window=pyqtSignal(str)
    def init(self):
    super().init()

        self.initUI()
    
    def initUI(self):
        #Initialize
        self.setGeometry(1000, 300, 1200, 800)
        self.setWindowTitle('Sensorlyze')
        self.setWindowIcon(QIcon('biosensor.jpg'))
        icon = QIcon('biosensor.jpg')
    
        # Add Text
        l1= QLabel("Welcome to SensorLyze",self)
        l1.move(25, 350)
        # l1.setWordWrap(True)
        l1.setFont(QFont('Calibri',15))
        l1.adjustSize()
        l2 = QLabel("A software to simply sensor analytics", self)
        l2.move(25, 400)
        l2.setFont(QFont('Calibri', 10))
        l2.adjustSize()
    
        #Add Buttons
        button1 = QPushButton('Start',self)
        button1.resize(button1.sizeHint())
        button1.clicked.connect(start_clicked)
        button1.move(60, 450)
        button2 = QPushButton('Exit', self)
        button2.resize(button2.sizeHint())
        button2.clicked.connect(exit_clicked)
        button2.move(240, 450)
    

    stylesheet = """
    QMainWindow {
    background-image: url("C:/Users/admin/Desktop/Sensorlyze/biosensor.jpg");
    background-repeat: no-repeat;
    background-position: center;
    }
    """

    def switch(self):

    self.switch_window.emit(self.line_edit.text())

    def start_clicked():
    window=QMainWindow()
    window.setGeometry(300, 500, 500, 500)
    window.setWindowTitle('Hello')
    window.show()
    win.hide()

    def exit_clicked():
    msgBox=QMessageBox()
    msgBox.setIcon(QMessageBox.Information)
    msgBox.setText("Are you sure you want to exit?")
    msgBox.setWindowTitle("Exit Sensorlyze")
    msgBox.setStandardButtons(QMessageBox.Ok|QMessageBox.Cancel)
    msgBox.buttonClicked.connect(msgButtonClick)
    returnValue = msgBox.exec()

    if returnValue==QMessageBox.Ok:
        exit()
    

    def msgButtonClick(i):
    print("Buttonclickedis:",i.text())

    def main():
    app = QApplication(sys.argv)
    app.setStyleSheet(stylesheet) # <---
    win=MainWindow()
    win.show()
    sys.exit(app.exec_())

    if name == 'main':
    main()


  • Lifetime Qt Champion

    Hi and welcome to the forums
    What do you mean ?
    So when you click a button and this is run
    def start_clicked():
    window=QMainWindow()
    window.setGeometry(300, 500, 500, 500)
    window.setWindowTitle('Hello')
    window.show()
    win.hide()

    the win Is closed or what you mean cant keep open ?


  • Qt Champions 2019

    @smoussa said in Cant keep a second window open when pressing on a button:

    def start_clicked():
    window=QMainWindow()

    You're creating a local variable (window) which is destroyed as soon as start_clicked() terminates...


Log in to reply