为什么我的应用程序在PyQt5中打开一个新窗口时关闭?

时间:2017-10-24 00:00:42

标签: python pyqt pyqt5

~EDIT(原始问题仍在下面)〜当我在新窗口中删除self.setGeometry()调用时,它应该正常工作。这是为什么?我仍在为它构建用户界面,但是一旦我这样做,我希望我不会继续遇到这个问题......

~EDIT 2~刚才意识到它应该是self.resize()而不是self.setGeometry()......

self.solved()

:(

我只是在学习PyQt5并且只是做了一些麻烦。出于某种原因,当我尝试从主应用程序窗口打开一个新窗口时,整个事情就会关闭。放入一些印刷语句来跟踪进度表明它实际上并没有创建新窗口。

主窗口代码:

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, QApplication
from newLeague import *


class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()

        newLeagueAction = QAction('Create New League', self)
        newLeagueAction.setShortcut('Ctrl+N')
        newLeagueAction.setStatusTip('Create a new league from scratch')
        newLeagueAction.triggered.connect(self.createNewLeague)

        openLeagueAction = QAction('Open Existing League', self)
        openLeagueAction.setShortcut('Ctrl+E')
        openLeagueAction.setStatusTip('Continue with a previously started league')
        openLeagueAction.triggered.connect(self.openExistingLeague)

        exitAction = QAction('Quit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Quit the application...')
        exitAction.triggered.connect(self.close)

        self.statusBar()

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(newLeagueAction)
        fileMenu.addAction(openLeagueAction)
        fileMenu.addAction(exitAction)

        self.resize(1920, 1080)
        self.setWindowTitle("Brackets")

    def createNewLeague(self):
        '''shows dialog to create a new league'''

        self.newLeague = CreateLeague()
        self.newLeague.show()
        print('New League being created...')

    def openExistingLeague(self):
        print('Existing League opening...')


def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

这是第二个窗口:

from PyQt5.QtWidgets import QMainWindow


class CreateLeague(QMainWindow):
    def __init__(self):
        super(CreateLeague, self).__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(600, 500)
        self.setWindowTitle('Create A New League')

我已经查看了其他示例,例如thisthis,而且我看不出它是什么,我做了不同的事情。我已尝试在构造函数中使用parent作为参数,结果没有区别。

1 个答案:

答案 0 :(得分:0)

您的主窗口代码没问题,但您应该从第二个窗口中的CreateLeague, self参数中删除super个参数,然后,您的代码应该可以正常工作。

见下文:

from PyQt5.QtWidgets import QMainWindow
class CreateLeague(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.resize(600, 500)
        self.setWindowTitle('Create A New League') 
相关问题