亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

飲歌長嘯 2023-03-16 17:03:21
我正在嘗試構建一個基于 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())我在這里錯過了什么嗎?任何幫助...
查看完整描述

1 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

Alec 回答了這個問題,但如果您仍然不清楚,這里是更正后的代碼。


import sys

from PyQt5.QtWidgets import QMainWindow, QLabel, QPushButton, QMessageBox, QApplication

from PyQt5.QtCore import pyqtSignal, pyqtSlot

from PyQt5.QtGui import QIcon, QFont

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(self.start_clicked)

        button1.move(60, 450)

        button2 = QPushButton('Exit', self)

        button2.resize(button2.sizeHint())

        button2.clicked.connect(self.exit_clicked)

        button2.move(240, 450)


    def start_clicked(self):

        self.window = QMainWindow()

        self.window.setGeometry(300, 500, 500, 500)

        self.window.setWindowTitle('Hello')

        self.window.show()

        # win.hide()


    def exit_clicked(self):

        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(self.msgButtonClick)

        returnValue = msgBox.exec()


        if returnValue == QMessageBox.Ok:

            exit()


    def msgButtonClick(self, i):

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



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

    app = QApplication(sys.argv)

    app.setStyleSheet(stylesheet)     # <---

    win=MainWindow()

    win.show()

    sys.exit(app.exec_())


if __name__ == '__main__':

    main()



查看完整回答
反對 回復 2023-03-16
  • 1 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號