按下pyqt5后无法打开新窗口

时间:2017-12-05 18:08:35

标签: python input error-handling window pyqt5

我学习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_())

2 个答案:

答案 0 :(得分:1)

您需要保存对窗口的引用,否则在on_click()完成执行时将对其进行垃圾回收。最简单的方法是使用App

将其存储在self
@pyqtSlot()
def on_click(self):
    self.ex1 = App1()

答案 1 :(得分:0)

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