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

时间:2016-12-14 19:22:46

标签: python python-3.x pyqt pyqt4

我刚开始编程,开始使用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()

0 个答案:

没有答案