Question

Currently I am using tkinter's askopenfilename in a quicklist editor for Ubuntu to get a file's name and location. Although it works fine, the look and feel is not native.

Is there an easy alternative dialogue window, to navigate and get a file's name and location?

Was it helpful?

Solution 2

Zenity

Zenity's File Selection Dialog provides an easy and natively looking solution with the --file-selection option. The dialog provides a number of options.

See also Zenity's man pages.

In its simplest form:

enter image description here


#!/usr/bin/env python3
import subprocess

try:
    file = subprocess.check_output(["zenity", "--file-selection"]).decode("utf-8").strip()
    print(file)
except subprocess.CalledProcessError:
    pass

Gtk's FileChooserDialog

Another option is Gtk's FileChooserDialog, which produces, as one might expect, perfectly natively looking file chooser dialog windows.

OTHER TIPS

You could try with wxPython FileDialog:

>>> import wx
>>> d = wx.FileDialog(None)
>>> d.ShowModal()
5101
>>> 

It gives a more OS specific look

wxPython is arriving soon to py3k as the Phoenix project and there are already snapshots for windows and mac (see my comment below). If you want something more stable you can use pyQt QtGui.QFileDialog.

import sys
from PyQt4 import QtGui

class Dialog(QtGui.QMainWindow):
    def __init__(self):
        super(Example, self).__init__()
        filename = QtGui.QFileDialog.getOpenFileName()
        print filename

app = QtGui.QApplication(sys.argv)
dialog = Dialog()

You have a more complete example here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top