QCoreApplication::exec:事件循环已在运行

2024-09-29 23:22:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始编程,开始使用Python和PyQt4。得到了这个错误: QCoreApplication::exec:事件循环已在运行

基本上,我想写一个函数,当你按下一个按钮,打开一个新的gui窗口(而之前的窗口在那一点上是不存在的)和全新的布局。在

有没有一种方法可以修复脚本或者用另一种方法编写脚本来获得结果?在

import sys
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
def __init__(self):
    super(Window, self).__init__()
    self.setGeometry(50, 50 , 1280, 720)
    self.setWindowTitle("Main")
    self.setWindowIcon(QtGui.QIcon("smili.png"))

    extractAction = QtGui.QAction("Exit", self)
    extractAction.setShortcut("Ctrl+Q")
    extractAction.setStatusTip("Press to leave the app.")
    extractAction.triggered.connect(self.close_appllication)
    self.statusBar()

    mainMenu = self.menuBar()
    fileMenu = mainMenu.addMenu("Menu")
    fileMenu.addAction(extractAction)
    self.home()

def home(self):
    btn = QtGui.QPushButton("Quit", self)
    btn.clicked.connect(QtCore.QCoreApplication.instance() .quit)
    btn.resize(250,50)
    btn.move(100,600)
    btn.setStyleSheet('QPushButton{background-color:#8A0808;color:#000000;font-size:25px;border:5px}')

    btn = QtGui.QPushButton("Start", self)
    btn.clicked.connect(self.redirect_window)
    btn.resize(250,50)
    btn.move(100,200)
    btn.setStyleSheet('QPushButton{background-color:#8A0808;color:#000000;font-size:25px;border:5px}')

    extractAction = QtGui.QAction(QtGui.QIcon("exitb.png"), "Exit the application.", self)
    extractAction.triggered.connect(self.close_appllication)

    self.toolBar = self.addToolBar("ToolBar")
    self.toolBar.addAction(extractAction)

    checkBox = QtGui.QCheckBox("Enlarge Window", self)
    checkBox.move(100, 25)
    checkBox.stateChanged.connect(self.enlarge_window)

    self.show()

def redirect_window(self):
    class startWindow(QtGui.QMainWindow):
        def __init__(self):
            super(Window, self).__init__()
            self.setGeometry(50, 50 , 1280, 720)
            self.setWindowTitle("Main/Start")
            self.setWindowIcon(QtGui.QIcon("smili.png"))

            extractAction = QtGui.QAction("Exit", self)
            extractAction.setShortcut("Ctrl+Q")
            extractAction.setStatusTip("Press to leave the app.")
            extractAction.triggered.connect(self.close_appllication)
            self.statusBar()

            mainMenu = self.menuBar()
            fileMenu = mainMenu.addMenu("Menu")
            fileMenu.addAction(extractAction)
            self.home()

    main()

def enlarge_window(self, state):
    if state == QtCore.Qt.Checked:
        self.setGeometry(50,50,1920,1080)
    else:
        self.setGeometry(50,50,1280,720)

def close_appllication(self):
    print("Shutting down!")
    sys.exit()

def main(): 
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

main()

Tags: selfappcloseinitdefconnectsyswindow

热门问题