ipython / ipython/ipython

getopenfilename does not block when when using ipython inputhook/start_event_loop

Open
#7,301 11 comments 0 reactions 0 assignees View on GitHub
bug core osx
Dominant language
Python
Stars
16.8k
Forks
4.5k
Avg merge
1d 2h
Merged PRs (30d)
6

Description

This is very similar to #2380. However, since that issue, and related ones, are all closed, I'm presenting this similar issue, which has already been posted on [Stack Overflow](http://stackoverflow.com/questions/27625394/getopenfilename-does-not-block-when-when-using-ipython-inputhook-start-event-loo)

Note: If not using the native dialog option, all works fine. This is just when using the native dialog.

When trying to run Qt in iPython, using the inputhook and start_event_loop, to create non-blocking app, the function PyQt4.QtGui.QFileDialog.getOpenFileName does not allow for input. It pops up and immediately closes.

The code below demonstrates the behavior. If run through _main(), which starts the Qt eventloop normally, all works fine. Both the generic Dialog and FileDialog operate as expected.

However, when run as

```
$ ipython
In [1]: import ipython_nonblock_example as nb
In [2]: nb_instance = nb.start_gui(nb.Example)
```

the Dialog works as expected. But the FileDialog does the open/close behavior.
Versions are: IPython 2.3.1, Qt 4.8, PyQt4 4.11.1,Python 2.7.5

File ipython_nonblock_example.py

``` python
import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()

self.initUI()

def initUI(self):
self.btn = QtGui.QPushButton('Dialog', self)
self.btn.move(20, 20)
self.btn.clicked.connect(self.showDialog)

self.btn2 = QtGui.QPushButton('File', self)
self.btn2.move(20,50)
self.btn2.clicked.connect(self.showFile)

self.le = QtGui.QLineEdit(self)
self.le.move(130, 22)

self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Input dialog')
self.show()

def showDialog(self):
text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',
'Enter your name:')

if ok:
self.le.setText(str(text))

def showFile(self):
file_name = str(QtGui.QFileDialog.getOpenFileName(parent=self, caption='Open file'))

if file_name:
self.le.setText(str(file_name))

def start_gui(ui, block=False):
try:
if not block & __IPYTHON__:
from IPython.lib.inputhook import enable_gui
app = enable_gui('qt4')
else:
raise ImportError
except (ImportError, NameError):
app = QtCore.QCoreApplication.instance()
if app is None:
app = QtGui.QApplication(sys.argv)

ui_instance = ui()

try:
from IPython.lib.guisupport import start_event_loop_qt4
start_event_loop_qt4(app)
return ui_instance
except ImportError:
app.exec_()

def _main():
start_gui(Example, block=True)

if __name__ == '__main__':
_main()
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.