IPython Notebook:使用GUI打开/选择文件(Qt Dialog) [英] IPython Notebook: Open/select file with GUI (Qt Dialog)

查看:237
本文介绍了IPython Notebook:使用GUI打开/选择文件(Qt Dialog)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您在笔记本上对不同的数据文件执行相同的分析时,可以方便地以图形方式选择数据文件。

When you perform the same analysis in a notebook on different data files, may be handy to graphically select a data file.

在我的python脚本中,我通常实现一个QT对话框,返回所选文件的文件名:

In my python scripts I usually implement a QT dialog that returns the file-name of the selected file:

from PySide import QtCore, QtGui

def gui_fname(dir=None):
    """Select a file via a dialog and return the file name.
    """
    if dir is None: dir ='./'
    fname = QtGui.QFileDialog.getOpenFileName(None, "Select data file...", 
            dir, filter="All files (*);; SM Files (*.sm)")
    return fname[0]

但是,从笔记本运行此函数

However, running this function from an notebook

full_fname = gui_fname()

导致内核死亡(并重新启动):

causes the kernel to die (and restart):

有趣的是,将3个命令放在3个单独的单元格中工作

Interestingly, puttying this 3 command in 3 separate cells works

%matplotlib qt
full_fname = gui_fname()
%matplotlib inline

但是当我将这些命令放在一个单元中时,内核会再次死掉。

but when I put those commands in one single cell the kernel dies again.

这可以防止创建像 gui_fname_ipynb()这样透明地允许用GUI选择文件的函数。

This prevents to create a function like gui_fname_ipynb() that transparently allows selecting a file with a GUI.

为方便起见,我创建了一个说明问题的笔记本:

For convenience, I created a notebook illustrating the problem:

  • Open/select file with GUI (Qt Dialog)

有关如何从IPython Notebook中执行文件选择对话框的任何建议吗?

Any suggestion on how to execute a dialog for file selection from within an IPython Notebook?

推荐答案

在Windows上使用Anaconda 5.0.0 (Python 3.6.2,IPython 6.1.0),以下两个选项都适用于我。

Using Anaconda 5.0.0 on windows (Python 3.6.2, IPython 6.1.0), the following two options are both working for me.

CELL 1:

%gui qt

from PyQt5.QtWidgets import QFileDialog

def gui_fname(dir=None):
    """Select a file via a dialog and return the file name."""
    if dir is None: dir ='./'
    fname = QFileDialog.getOpenFileName(None, "Select data file...", 
                dir, filter="All files (*);; SM Files (*.sm)")
    return fname[0]

CELL 2:

gui_fname()

这对我有用但似乎有点......脆弱。如果我将这两个东西组合到同一个单元格中,它会崩溃。或者如果我省略%gui qt ,它崩溃。如果我重新启动内核并运行所有单元格,它就不起作用。所以我有点像这个其他选项...

This is working for me but it seems a bit...fragile. If I combine these two things into the same cell, it crashes. Or if I omit the %gui qt, it crashes. If I "restart kernel and run all cells", it doesn't work. So I kinda like this other option...

(基于mkrog代码这里。)

(Based on mkrog code here.)

将以下单独的PYTHON SCRIPT写入 blah.py

PUT THE FOLLOWING IN A SEPARATE PYTHON SCRIPT CALLED blah.py:

from sys import executable, argv
from subprocess import check_output
from PyQt5.QtWidgets import QFileDialog, QApplication

def gui_fname(directory='./'):
    """Open a file dialog, starting in the given directory, and return
    the chosen filename"""
    # run this exact file in a separate process, and grab the result
    file = check_output([executable, __file__, directory])
    return file.strip()

if __name__ == "__main__":
    directory = argv[1]
    app = QApplication([directory])
    fname = QFileDialog.getOpenFileName(None, "Select a file...", 
            directory, filter="All files (*)")
    print(fname[0])

...并在你的JUPYTER笔记本中

...AND IN YOUR JUPYTER NOTEBOOK

import blah
blah.gui_fname()

这篇关于IPython Notebook:使用GUI打开/选择文件(Qt Dialog)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆