0

I've noticed that, using the following code, if you choose to click "Cancel" on the FileDialog for "Import File", the entire application closes instead of returning to the menu and awaiting a different choice. How can I get it to just return to the mainMenu?

(Note: I've noticed if I don't put anything after the initial file dialog call, it functions fine.)

Code:

import sys
import xml.etree.ElementTree as et
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *


class MainMenu(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        layout = QGridLayout()

        # Create Objects for Main Menu
        logoLabel = QLabel("TESTAPP")
        logoFont = QFont("Broadway", 48)
        logoLabel.setFont(logoFont)
        versionLabel = QLabel("Version 0.1a")
        copyrightLabel = QLabel("Copyright 2016")
        importButton = QPushButton("Import File")
        quitButton = QPushButton("Quit")

        # Set Locations of Widgets
        layout.addWidget(logoLabel, 0, 0)
        layout.addWidget(versionLabel, 1, 0)
        layout.addWidget(copyrightLabel, 2, 0)
        layout.addWidget(importButton, 4, 0)
        layout.addWidget(quitButton, 5, 0)
        self.setLayout(layout)
        self.setWindowTitle("NESSQL")

        # Connect Buttons to Actions
        importButton.clicked.connect(self.importFile)
        quitButton.clicked.connect(self.close)

    def importFile(self):
        # Open dialog box to get the filename
        file = QFileDialog.getOpenFileName(self, caption="File to Import", directory=".",
                                           filter="All Files (*.*)")
        data = et.parse(file)
        root = data.getroot()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainMenu = MainMenu()
    mainMenu.show()
    app.exec_()
5
  • using PyQt4 its returning to main menu and waiting for choice, wierd
    – Harwee
    May 25, 2016 at 4:55
  • @Harwee noticed this myself but am forced to use PyQt5 for this project. May 25, 2016 at 4:57
  • @Harwee - looks like it was only happening when I have my additional code after that initial call to the QFileDialog to get the path/filename. May 25, 2016 at 5:19
  • I think the problem is with et.parse but that should raise IO error. when you press cancel QFileDialog returns '' you are not handling empty url and passing it directly to et.parse()
    – Harwee
    May 25, 2016 at 5:39
  • I seem to get the same results regardless of what I put there. I had it go to a function that simply had pass and it gave me the same. May 25, 2016 at 20:27

2 Answers 2

2

by keeping condition for checking whether file is empty or not you can avoid this issue. It goes something like this in your case.

def importFile(self):
    # Open dialog box to get the filename
    file = QFileDialog.getOpenFileName(self, caption="File to Import", 
                              directory=".",filter="All Files (*.*)")

    if not file[0] == "":
        data = et.parse(file)
        root = data.getroot()
2

saran vaddi's answer didn't work for me but this did:

def importFile(self):
        # Open dialog box to get the filename
        file = QFileDialog.getOpenFileName(self, caption="File to Import", directory=".", filter="All Files (*.*)")

        # If file is blank, simply return control to calling function
        if not file:
            return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.