简体   繁体   中英

Why does my app close when opening a new window in PyQt5?

~EDIT (original question still below)~ when I remove the self.setGeometry() call in the new window it works as it should. Why is that? I'm still building out the UI for it, but once I do I hope I don't continue to have this problem...

~EDIT 2~ Just realized it should be self.resize() not self.setGeometry()...

self.solved()

:(

I'm just learning PyQt5 and just doing a little messing around. For some reason when I try to open a new window from the main application window, the whole thing closes. Putting in some print statements to track progress shows that it's not actually creating the new window either.

Main Window code:

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

Here is the second window:

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')

I've looked at other examples such as this , and this , and I'm not seeing what it is I'm doing different. I've experimented with using parent as an argument in the constructors and the result is no different.

Your Main Window code is ok, but you should remove CreateLeague, self arguments from the super parameters in your second window, then, your code should work fine.

See below:

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') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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