【问题标题】:failed to open a new window after push button pyqt5按下按钮pyqt5后无法打开新窗口
【发布时间】:2017-12-05 18:08:35
【问题描述】:

我学习 pyqt5 并尝试通过单击另一个窗口中的按钮来打开一个新窗口。如果没有 input() 函数,它将立即关闭打开的窗口,所以我输入 input() 以保持窗口打开。窗口将打开很长时间,但随后停止工作。有人能帮我吗 ?谢谢

import sys
from PyQt5.QtWidgets import QApplication, QPushButton,QMainWindow,QWidget
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot


class App1(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'open window'
        self.left = 60
        self.top = 60
        self.width = 320
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.show()
        m.getch()
        input()                  '''here is the problem'''

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 button - pythonspot.com'
        self.left = 200
        self.top = 200
        self.width = 320
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('PyQt5 button', self)
        button.setToolTip('This is an example button')
        button.move(100, 70)
        button.clicked.connect(self.on_click)

        self.show()

    @pyqtSlot()
    def on_click(self):

        app1 = QApplication(sys.argv)
        ex1 = App1()
        sys.exit(app1.exec_())



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

【问题讨论】:

    标签: python input error-handling window pyqt5


    【解决方案1】:

    您需要保存对窗口的引用,否则当on_click() 执行完毕时它将被垃圾回收。最简单的方法是通过使用self将其存储在App

    @pyqtSlot()
    def on_click(self):
        self.ex1 = App1()
    

    【讨论】:

    • 嗨 user3419537 我更正了代码,但仍然出现“停止工作错误”
    • 您不再需要input() 行了。您还需要删除 on_click() 中的代码,在其中创建一个新的 QApplication - Qt 程序中只能有一个实例。
    【解决方案2】:
    import sys
    from PyQt5.QtWidgets import QApplication, QPushButton,QMainWindow,QWidget
    from PyQt5.QtGui import QIcon
    from PyQt5.QtCore import pyqtSlot
    
    class App1(QWidget):
    
        def __init__(self):
            super().__init__()
            self.title = 'open window'
            self.left = 60
            self.top = 60
            self.width = 320
            self.height = 200
            self.initUI()
    
        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
    
            self.show()
            input()
    
    
    class App(QMainWindow):
    
        def __init__(self):
            super().__init__()
            self.title = 'PyQt5 button - pythonspot.com'
            self.left = 200
            self.top = 200
            self.width = 320
            self.height = 200
            self.initUI()
    
        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
    
            button = QPushButton('PyQt5 button', self)
            button.setToolTip('This is an example button')
            button.move(100, 70)
            button.clicked.connect(self.on_click)
    
            self.show()
    
    
        @pyqtSlot()
        def on_click(self):
            self.ex1 = App1()
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
    
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 2020-12-13
      • 2015-09-09
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 2011-06-06
      相关资源
      最近更新 更多