【问题标题】:How to keep a window open in Python PyQt如何在 Python PyQt 中保持窗口打开
【发布时间】:2020-11-08 11:45:18
【问题描述】:

我正在尝试构建一个基于 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())


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

if __name__ == '__main__':
    main()

我在这里遗漏了什么吗?任何帮助...

【问题讨论】:

  • 提供的代码中存在语法错误。可以修一下吗?
  • 没有父集,也没有为window 保留指针,因此它在函数期间创建,然后在函数返回时立即销毁并收集垃圾。使start_clicked 函数成为类的方法,并使用父window = QMainWindow(self) 设置窗口或保留指向它的指针self.window = QMainWindow()
  • 嗨@alec,我不确定你的意思:使start_clicked函数成为类的方法。能详细点吗?
  • 使其成为像initUI这样的常规实例方法

标签: python pyqt5


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-14
    • 2021-06-30
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 2021-11-15
    相关资源
    最近更新 更多