简体   繁体   English

按下按钮pyqt5后无法打开新窗口

[英]failed to open a new window after push button pyqt5

i learning pyqt5 and trying to open a new window by click a button from another window. 我学习pyqt5并尝试通过单击另一个窗口中的按钮来打开新窗口。 if there are not input() function it will close the open window immediately, so i put input() to keep the window open. 如果没有input()函数,它将立即关闭打开的窗口,所以我放了input()来保持窗口打开。 the window will open for long, but then it has stopped working. 该窗口将打开很长时间,但随后已停止工作。 Can some one help me ? 有人能帮我吗 ? Thank you 谢谢

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

You need to save a reference to the window or it will be garbage collected when on_click() finishes executing. 您需要保存对窗口的引用,否则在on_click()完成执行时将被垃圾回收。 The easiest way is to store it in App through the use of self 最简单的方法是通过使用self将其存储在App

@pyqtSlot()
def on_click(self):
    self.ex1 = App1()
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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM