Search code examples
python-3.xubuntutkinterfile-browser

Alternative for tkinter's askopenfilename


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?


Solution

  • 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.