如何在 Python PyQt 中保持窗口打开

我正在尝试构建一个基于 python 的软件。(基于 PYQT 的软件)

问题:

  • 我的第二个窗口在打开后立即关闭。

问题:

  • 我的代码有问题吗?

  • 我如何解决它?

注意:单击开始按钮时会打开第二个窗口。

这是我的代码:

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


我在这里错过了什么吗?任何帮助...


饮歌长啸
浏览 184回答 1
1回答

千巷猫影

Alec 回答了这个问题,但如果您仍然不清楚,这里是更正后的代码。import sysfrom PyQt5.QtWidgets import QMainWindow, QLabel, QPushButton, QMessageBox, QApplicationfrom PyQt5.QtCore import pyqtSignal, pyqtSlotfrom PyQt5.QtGui import QIcon, QFontclass MainWindow(QMainWindow):&nbsp; &nbsp; switch_window=pyqtSignal(str)&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__()&nbsp; &nbsp; &nbsp; &nbsp; self.initUI()&nbsp; &nbsp; def initUI(self):&nbsp; &nbsp; &nbsp; &nbsp; #Initialize&nbsp; &nbsp; &nbsp; &nbsp; self.setGeometry(1000, 300, 1200, 800)&nbsp; &nbsp; &nbsp; &nbsp; self.setWindowTitle('Sensorlyze')&nbsp; &nbsp; &nbsp; &nbsp; self.setWindowIcon(QIcon('biosensor.jpg'))&nbsp; &nbsp; &nbsp; &nbsp; icon = QIcon('biosensor.jpg')&nbsp; &nbsp; &nbsp; &nbsp; # Add Text&nbsp; &nbsp; &nbsp; &nbsp; l1= QLabel("Welcome to SensorLyze",self)&nbsp; &nbsp; &nbsp; &nbsp; l1.move(25, 350)&nbsp; &nbsp; &nbsp; &nbsp; # l1.setWordWrap(True)&nbsp; &nbsp; &nbsp; &nbsp; l1.setFont(QFont('Calibri',15))&nbsp; &nbsp; &nbsp; &nbsp; l1.adjustSize()&nbsp; &nbsp; &nbsp; &nbsp; l2 = QLabel("A software to simply sensor analytics", self)&nbsp; &nbsp; &nbsp; &nbsp; l2.move(25, 400)&nbsp; &nbsp; &nbsp; &nbsp; l2.setFont(QFont('Calibri', 10))&nbsp; &nbsp; &nbsp; &nbsp; l2.adjustSize()&nbsp; &nbsp; &nbsp; &nbsp; #Add Buttons&nbsp; &nbsp; &nbsp; &nbsp; button1 = QPushButton('Start',self)&nbsp; &nbsp; &nbsp; &nbsp; button1.resize(button1.sizeHint())&nbsp; &nbsp; &nbsp; &nbsp; button1.clicked.connect(self.start_clicked)&nbsp; &nbsp; &nbsp; &nbsp; button1.move(60, 450)&nbsp; &nbsp; &nbsp; &nbsp; button2 = QPushButton('Exit', self)&nbsp; &nbsp; &nbsp; &nbsp; button2.resize(button2.sizeHint())&nbsp; &nbsp; &nbsp; &nbsp; button2.clicked.connect(self.exit_clicked)&nbsp; &nbsp; &nbsp; &nbsp; button2.move(240, 450)&nbsp; &nbsp; def start_clicked(self):&nbsp; &nbsp; &nbsp; &nbsp; self.window = QMainWindow()&nbsp; &nbsp; &nbsp; &nbsp; self.window.setGeometry(300, 500, 500, 500)&nbsp; &nbsp; &nbsp; &nbsp; self.window.setWindowTitle('Hello')&nbsp; &nbsp; &nbsp; &nbsp; self.window.show()&nbsp; &nbsp; &nbsp; &nbsp; # win.hide()&nbsp; &nbsp; def exit_clicked(self):&nbsp; &nbsp; &nbsp; &nbsp; msgBox = QMessageBox()&nbsp; &nbsp; &nbsp; &nbsp; msgBox.setIcon(QMessageBox.Information)&nbsp; &nbsp; &nbsp; &nbsp; msgBox.setText("Are you sure you want to exit?")&nbsp; &nbsp; &nbsp; &nbsp; msgBox.setWindowTitle("Exit Sensorlyze")&nbsp; &nbsp; &nbsp; &nbsp; msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)&nbsp; &nbsp; &nbsp; &nbsp; msgBox.buttonClicked.connect(self.msgButtonClick)&nbsp; &nbsp; &nbsp; &nbsp; returnValue = msgBox.exec()&nbsp; &nbsp; &nbsp; &nbsp; if returnValue == QMessageBox.Ok:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit()&nbsp; &nbsp; def msgButtonClick(self, i):&nbsp; &nbsp; &nbsp; &nbsp; print("Buttonclickedis:", i.text())stylesheet = """&nbsp; &nbsp; QMainWindow {&nbsp; &nbsp; background-image: url("C:/Users/admin/Desktop/Sensorlyze/biosensor.jpg");&nbsp;&nbsp; &nbsp; background-repeat: no-repeat;&nbsp;&nbsp; &nbsp; background-position: center;&nbsp; &nbsp; }"""# def switch(self):#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.switch_window.emit(self.line_edit.text())def main():&nbsp; &nbsp; app = QApplication(sys.argv)&nbsp; &nbsp; app.setStyleSheet(stylesheet)&nbsp; &nbsp; &nbsp;# <---&nbsp; &nbsp; win=MainWindow()&nbsp; &nbsp; win.show()&nbsp; &nbsp; sys.exit(app.exec_())if __name__ == '__main__':&nbsp; &nbsp; main()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python