3

I am kind of new to Python and especially new to PyQT. I Used PyQT5 to create a very simple gui. Now, I would like to upgrade it to involve something more real then to calculate number operations.

I want a user to choose a directory with images. After that and some other small operations like ticking some check boxes I want to run an algorithm of mine in the background and in the mean while to show him/her the progress via a progressBar.

This is my code right now:

import sys
from os.path import expanduser

from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic, QtGui

Ui_MainWindow, QtBaseClass = uic.loadUiType("mainGui_3A.ui")


class MyApp(QMainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.pushButton_Directory.clicked.connect(self.choose_directory)
        self.ui.pushButton_CreateAlbum.clicked.connect(self.create_album)

    def choose_directory(self):
        my_dir = QtGui.QFileDialog.getExistingDirectory(
        self,
        "Open a folder",
        expanduser("~"),
        QtGui.QFileDialog.ShowDirsOnly
    )
        self.ui.lineEdit_Directory.setText(my_dir)

    def create_album(self):
        current_dir = self.ui.lineEdit_Directory.toPlainText()
        check1 = self.ui.checkBox_1.value()
        check2 = self.ui.checkBox_2.value()
        return current_dir, check1, check2

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

I have looked at :

PYQT - How to open a directory folder?

PyQt: QFileDialog.getExistingDirectory using a default directory, user independant

as you can see I still didn't add the last bit since the error below but I think I need to look into:

Example of the right way to use QThread in PyQt?

Change the value of the progress bar from a class other than my GUI class PyQt4

There is no error at first. the ui is loaded currectly I believe but as soon as I click one of my buttons the program stop running.. what did I do wrong? do I need to use threads to choose the directory?

Any tips for the future would be welcomed!

Thanks in advance!

Daniel Glazer
  • 123
  • 2
  • 10
  • What does it mean it stops running? It exists? I dont see anything obviously wrong except (i hope) invalid formatting - create_album should be a method of class MyApp, right? – Radosław Cybulski Jun 25 '17 at 20:50
  • 1
    when I click any of the buttons I get the known message that it's title is 'Python has stopped working' (from Windows) and then I am advise to close the program... this does bot happen when I click the checkBox. you are right and the create_album is a method in the class - formatting error. – Daniel Glazer Jun 25 '17 at 20:57
  • corrected formatting problem mentioned above – Daniel Glazer Jun 25 '17 at 21:00
  • I would advise against using threads - certainly not necessary for picking a directory - and, while very powerful, a steep learning curve. Are you running the program from a command prompt? Or an IDE? Are there any exception messages? – Joe P Jun 25 '17 at 21:45
  • not from the cmd but via IDE (PyCharm)... no exceptions all I get is: 'Process finished with exit code -1073740791 (0xC0000409)' – Daniel Glazer Jun 25 '17 at 22:01

1 Answers1

7

I don't like the idea of answering my own questions but I guess that if this could save someone several hours of research that its quite alright.

so here it is I found a solution and the possible error in my code.

Solution:

import sys
from os.path import expanduser

from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
from PyQt5 import uic, QtGui, QtCore

Ui_MainWindow, QtBaseClass = uic.loadUiType("mainGui_3A.ui")


class MyApp(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        logo_src = "appLogo.png"
        self.setWindowIcon(QtGui.QIcon(logo_src))
        self.ui.pushButton_Directory.clicked.connect(self.choose_directory)
        self.ui.pushButton_CreateAlbum.clicked.connect(self.create_album)
        self.ui.pushButton_Directory.setToolTip('Choose directory')
        self.ui.checkBox_Quality.setToolTip('Include image quality assessment upon selection of representative photos')
        self.ui.checkBox_Launch.setToolTip('Display output album when done')

    def choose_directory(self):
        print("Hello1")
        input_dir = QFileDialog.getExistingDirectory(None, 'Select a folder:', expanduser("~"))
        self.ui.lineEdit_Directory.setText(input_dir)




    def create_album(self):
        print("Hello2")
        current_dir = self.ui.lineEdit_Directory.text()
        if current_dir != "":
            quality = self.ui.checkBox_Quality.isChecked()
            launch = self.ui.checkBox_Launch.isChecked()
            print(current_dir)
            print(quality)
            print(launch)
        return 1


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

possible error I found via text comparison:

  • Using

    my_dir = QtGui.QFileDialog.getExistingDirectory(
        self,
        "Open a folder",
        expanduser("~"),
        QtGui.QFileDialog.ShowDirsOnly
    )
    

instead of

    input_dir = QFileDialog.getExistingDirectory(None, 'Select a folder:', 
        expanduser("~"))
Daniel Glazer
  • 123
  • 2
  • 10