thescoop
thescoop

Reputation: 514

Python : Dialog to browse for a directory (loading PyQt4 gives an error, and kernel dies)

enter image description here

I'm using Canopy version 1.6.4 (latest version as of April 2016) and I simply wish to make a dialogue to ask the user browse to a Folder (directory) in a pop-up window, and for python to take the folder name and path as a variable.

Whatever I try when using PyQt4, I keep getting the following error message:

ImportError: Importing PyQt4 disabled by IPython, which has already imported an Incompatible QT Binding: pyside

I've found lots of other people experiencing a similar thing (e.g.: How can I use Pyqt with Enthought Canopy and this answer which I found incomprehensible : https://github.com/ipython/ipython/issues/2955/ ), but no simple answer as to how to solve this (I'm pretty new to the Python and Python environments). Can anyone recommend a quick fox, or better still another way of making a simple x-platform (Mac and windows) Dialogue box (Tkinter doesn't work either on Canopy!!!).

This enclosed screenshot isn't for a browser window, but this gives the same error message; as does Jupiter notebook and iPython.

FYI: Even without importing PySide I get this error! (I imported it once only, but not since).

Thanks!

Upvotes: 0

Views: 997

Answers (1)

Brendan Abel
Brendan Abel

Reputation: 37539

There are two different python bindings for Qt -- PyQt and PySide. You cannot use both at the same time. You cannot even import both of them into the same python session. I'm guessing you are launching this from within an embedded python console inside your IDE, which has chosen to use PySide (which is why you're getting this error).

You have two options.

  1. Use PySide instead of PyQt.
  2. Launch your script from a regular console (eg. cmd.exe, terminal, bash) instead of the embedded console inside your IDE (PySide won't be pre-loaded and you can use PyQt).

PyQt and PySide are very similar, and in most cases you can just change the import statements.

from PySide import QtGui, QtCore

For your original question of how to create a dialog to pick a directory, you can use QFileDialog.getExistingDirectory

import sys
from PySide import QtGui, QtCore


class Dialog(QtGui.QDialog):

    def __init__(self, parent):
        super(Dialog, self).__init__(parent)
        self.ui_lay = QtGui.QHBoxLayout()
        self.setLayout(self.ui_lay)
        self.ui_line = QtGui.QLineEdit(self)
        self.ui_lay.addWidget(self.ui_line)
        self.ui_btn = QtGui.QPushButton('...', self)
        self.ui_lay.addWidget(self.ui_btn)
        self.ui_btn.clicked.connect(self.browse)

    @QtCore.Slot()  # for pyqt, QtCore.pyqtSlot()
    def browse(self):
        path = QtGui.QFileDialog.getExistingDirectory(self, 'Pick a Folder')
        if path:
            self.ui_line.setText(path)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dlg = Dialog(None)
    dlg.show()
    app.exec_()

Upvotes: 1

Related Questions