Gtk+3错误:未定义“GLocalFile”

2024-09-29 06:32:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用以下方法在文件管理器中插入侧栏:

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

但是每当我运行这个代码:

NameError: name 'GLocalFile' is not defined

我试着调试它,注意到以下几点:

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

location是一个类型为GLocalFile的参数,因此看到这一点,我也尝试了__gi__.GLocalFile,正如预期的那样,它给出了:

NameError: name '__gi__' is not defined

通过在线搜索,我看到每个人都在使用相同的代码作为侧边栏,那么我在这里遗漏了什么呢?你知道吗


Tags: fromimportgtkgetonrepositorydeflocation
1条回答
网友
1楼 · 发布于 2024-09-29 06:32:07

GLocalFile是一个仅限内部的类,因此即使它显示在调试消息中,也不能在Python中访问它。你知道吗

NameError: name 'GLocalFile' is not defined很有意义,因为程序中没有任何东西可以导入该名称。)

根据documentationGtk.PlacesSidebar.get_location()的返回类型是Gio.File,所以您可以调用location.get_uri()。你知道吗

相关问题 更多 >