简体   繁体   English

为什么其他窗口(GUI)在 Python 中运行时其他窗口(GUI)没有打开?

[英]Why other window(GUI) is not opening while other window(GUI) is running in Python?

I have a scenario where one window is running in python Pyqt5.我有一个场景,其中一个窗口在 python Pyqt5 中运行。 I want that when certain event happens another window also opens up.我希望当某个事件发生时,另一个窗口也会打开。

I have written a code which I suppose should run fine but when event occurs to open other GUI, I gets an error.我编写了一个我认为应该可以正常运行的代码,但是当事件发生以打开其他 GUI 时,我收到错误消息。 My code:我的代码:

class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        app = QApplication(sys.argv)
        win = QWidget()

        l1 = QLabel("URL:")
        url = QLineEdit()
        l3 = QLabel("Wait (sec):")
        wait = QLineEdit()
        l2 = QLabel("Iteration:")
        l2.move(20,100)
        global add1
        count = QLineEdit()
        fbox = QFormLayout()
        fbox.addRow(l1, url)
        fbox.addRow(l3, wait)
        vbox = QVBoxLayout()

        vbox.addWidget(count)
        fbox.addRow(l2, vbox)
        startButton=QPushButton("Start")

        fbox.addRow(startButton)
        startButton.clicked.connect(self.requests)
        win.setLayout(fbox)

        win.setWindowTitle("--")
        win.resize(300,200)
        win.show()
        sys.exit(app.exec_())

    def requests(self):

        for x in range(0,int(count.text())):
            //certain event happens here, which will cause other window to get open         
             self.dialog = PopUp(self)
             self.dialog.show()

    def stop(self):
        sys.exit()

class PopUp(QMainWindow):

    def __init__(self, parent=None):
        super(PopUp, self).__init__(parent)
        app = QApplication(sys.argv)
        win = QWidget()

        l1 = QLabel("URL:")
        nextUrl = QLineEdit()

        fbox = QFormLayout()
        fbox.addRow(l1, url)

        startButton = QPushButton("Go")

        fbox.addRow(startButton)
        startButton.clicked.connect(self.requests)
        win.setLayout(fbox)

        win.setWindowTitle("--")
        win.resize(300, 200)
        win.show()
        sys.exit(app.exec_())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

I see that code is right but I am getting an unknown error: QCoreApplication::exec: The event loop is already running I have searched on google and here in stack overflow but didn't get anything worthy.我看到代码是正确的,但我收到了一个未知错误: QCoreApplication::exec: The event loop is already running我已经在谷歌和堆栈溢出中搜索过,但没有得到任何有价值的东西。 Anyone knows that why this error comes and why is it coming in my code??任何人都知道为什么会出现此错误以及为什么会出现在我的代码中?

Each PyQt5 application must create one application object app = QApplication(sys.argv) .每个 PyQt5 应用程序必须创建一个应用程序对象app = QApplication(sys.argv) Remove from the class MainWindow and the class PopUp - app = QApplication (sys.argv) .class MainWindowclass PopUp - app = QApplication (sys.argv)删除。

sys.exit(app.exec_()) - the mainloop of the application, he must also be alone. sys.exit(app.exec_()) - 应用程序的主循环,他也必须是一个人。 Remove from the class MainWindow and the class PopUp - sys.exit(app.exec_())class MainWindowclass PopUp删除 - sys.exit(app.exec_())

I improved the readability of your example a bit.我稍微提高了您示例的可读性。

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *


class PopUp(QMainWindow):
    def __init__(self, x, url, parent=None):
        super(PopUp, self).__init__(parent)

        self.url = url.text()
        self.x   = x + 1

        self.setWindowTitle("-- PopUp {}  --".format(self.x))
        self.setGeometry(self.x*100, self.x*100, 300, 200)

        win = QWidget()
        self.setCentralWidget(win)

        nextUrl = QLineEdit(self.url)
        self.startButton = QPushButton("Go {}".format(self.x))

        fbox = QFormLayout(win)
        fbox.addRow(QLabel("URL:"), nextUrl)
        fbox.addRow(self.startButton)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.dialogs = []
        self.flag    = False

        win = QWidget()
        self.setCentralWidget(win)

        self.url    = QLineEdit()                           # + self
        wait        = QLineEdit()
        self.count  = QLineEdit()                           # + self
        startButton = QPushButton("Start")
        startButton.clicked.connect(self.requests)

        fbox = QFormLayout(win)         
        fbox.addRow(QLabel("URL:"),        self.url)
        fbox.addRow(QLabel("Wait (sec):"), wait)
        fbox.addRow(QLabel("Iteration:"),  self.count)        
        fbox.addRow(startButton)

    def requests(self):
        if self.dialogs and self.flag:
            _ = [ i.hide() for i in self.dialogs]
            self.dialogs = []

        for x in range(0, int(self.count.text())):             
            # certain event happens here, which will cause other window to get open         
            dialog = PopUp(x, self.url, self)
            dialog.startButton.clicked.connect(self.requests2)
            dialog.show()

            self.dialogs.append(dialog)
        self.flag = True    

    def stop(self):   # ?
        sys.exit()

    def requests2(self):    
        print("def requests2(self): clicked Button {}".format(self.sender().text()))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = MainWindow()
    main.setWindowTitle("-- Title --")
    main.resize(300,200)    
    main.show()
    sys.exit(app.exec_())

在此处输入图片说明

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

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