Search code examples
pythonuser-interfacegtk3

Gtk+3 error: 'GLocalFile' is not defined


I am using following way to insert side bar in my file manager:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio


def on_open_location(placessidebar, location, flags):
#        import pdb;
#        pdb.set_trace()
        location = placessidebar.get_location()

        print("Opened URI: %s" % (GLocalFile.get_uri(location)))


def create_side_bar():
    # side bar
    placessidebar = Gtk.PlacesSidebar()
    placessidebar.set_open_flags(Gtk.PlacesOpenFlags.NORMAL)
    placessidebar.connect("open-location", on_open_location)

    return placessidebar

But whenever I run this code:

NameError: name 'GLocalFile' is not defined

I tried debugging it and noticed following things:

location
<__gi__.GLocalFile object at 0x7f3ea42b5cf0 (GLocalFile at 0x1aaae40)>

location is a parameter that is of type GLocalFile, so seeing this I also tried __gi__.GLocalFile and as expected it gave:

NameError: name '__gi__' is not defined

By searching online I saw everyone is using the same code for side bar, so what am I missing here?


Solution

  • GLocalFile is an internal-only class, so even though it shows up in debug messages you can't access it in Python.

    (The NameError: name 'GLocalFile' is not defined makes sense, because there isn't anything in your program that imports that name.)

    According to the documentation, the return type of Gtk.PlacesSidebar.get_location() is Gio.File, so you can just call location.get_uri().