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

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

问题描述

当您在笔记本中对不同的数据文件执行相同的分析时,以图形方式选择数据文件可能会很方便.

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:

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

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

推荐答案

在 windows (Python 3.6.2, IPython 6.1.0) 上使用 Anaconda 5.0.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.

细胞 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]

细胞 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.)

将以下内容放在名为 blah.py 的单独 Python 脚本中:

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 对话框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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