如何将一个项目添加到GTK的“最近使用的” Python的文件列表? [英] How does one add an item to GTK's "recently used" file list from Python?

查看:156
本文介绍了如何将一个项目添加到GTK的“最近使用的” Python的文件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我能够成功读取 >最近使用的文件列表如下所示:

  from gi.repository import Gtk 
recent_mgr = Gtk.RecentManager.get_default ()
for item in recent_mgr.get_items():
print(item.get_uri())

这会打印出我在Nautilus中查看Recent时看到的相同文件列表,或查看GIMP等应用程序的文件对话框中的Recently Used位置。



然而,当我尝试添加这样的项目时(其中 /home/laurence/foo/bar.txt 是现有的文本文件) ...

  recent_mgr.add_item('file:///home/laurence/foo/bar.txt')

...该文件不会显示在Nautilus的最新部分或文件对话框中。它甚至不会显示在由 get_items()返回的结果中。



如何添加文件到GTK最近使用的Python文件列表?

解决方案

A Gtk.RecentManager 需要发出已更改信号,以便将更新写入C ++类的专用属性中。要在应用程序中使用 RecentManager 对象,您需要通过调用 Gtk.main 来启动事件循环:

  from gi.repository import Gtk 

recent_mgr = Gtk.RecentManager.get_default()
uri = r'file:/ path / to / my / file'
recent_mgr.add_item(uri)
Gtk.main()

如果您不调用 Gtk.main()更改

要回答@andlabs查询,为什么 RecentManager.add_item 返回布尔值是因为调用了 g_file_query_info_async 函数。回调函数 gtk_recent_manager_add_item_query_info 然后将mimetype,应用程序名称和命令收集到 GtkRecentData struct中,最后调用 gtk_recent_manager_add_full 。来源是此处



如果出现任何问题, add_item 已完成,那么方法只返回 True 如果调用的对象是 RecentManager ,并且uri不是 NULL ;和 False ,否则。



文件不准确:


返回 TRUE 如果新项目已成功添加到最近使用的资源列表中


返回 TRUE 仅意味着调用异步函数来处理添加新项目。



正如Laurence Gonsalves所建议的,以下伪同步运行:

  from gi.repository import Gtk,GObject 

recent_mgr = Gtk .RecentManager.get_default()
uri = r'file:/ path / to / my / file'
recent_mgr.add_item(uri)
GObject.idle_add(Gtk.main_quit)
Gtk.main()


I'm trying to add to the "recently used" files list from Python 3 on Ubuntu.

I am able to successfully read the recently used file list like this:

from gi.repository import Gtk
recent_mgr = Gtk.RecentManager.get_default()
for item in recent_mgr.get_items():
    print(item.get_uri())

This prints out the same list of files I see when I look at "Recent" in Nautilus, or look at the "Recently Used" place in the file dialog of apps like GIMP.

However, when I tried adding an item like this (where /home/laurence/foo/bar.txt is an existing text file)...

recent_mgr.add_item('file:///home/laurence/foo/bar.txt')

...the file does not show up in the Recent section of Nautilus or in file dialogs. It doesn't even show up in the results returned by get_items().

How can I add a file to GTK's recently used file list from Python?

解决方案

A Gtk.RecentManager needs to emit the changed signal for the update to be written in a private attribute of the C++ class. To use a RecentManager object in an application, you need to start the event loop by calling Gtk.main:

from gi.repository import Gtk

recent_mgr = Gtk.RecentManager.get_default()
uri = r'file:/path/to/my/file'
recent_mgr.add_item(uri)
Gtk.main()

If you don't call Gtk.main(), the changed signal is not emitted and nothing happens.

To answer @andlabs query, the reason why RecentManager.add_item returns a boolean is because the g_file_query_info_async function is called. The callback function gtk_recent_manager_add_item_query_info then gathers the mimetype, application name and command into a GtkRecentData struct and finally calls gtk_recent_manager_add_full. The source is here.

If anything goes wrong, it is well after add_item has finished, so the method just returns True if the object it is called from is a RecentManager and if the uri is not NULL; and False otherwise.

The documentation is inaccurate in saying:

Returns TRUE if the new item was successfully added to the recently used resources list

as returning TRUE only means that an asynchronous function was called to deal with the addition of a new item.

As suggested by Laurence Gonsalves, the following runs pseudo-synchronously:

from gi.repository import Gtk, GObject

recent_mgr = Gtk.RecentManager.get_default()
uri = r'file:/path/to/my/file'
recent_mgr.add_item(uri)
GObject.idle_add(Gtk.main_quit)
Gtk.main()

这篇关于如何将一个项目添加到GTK的“最近使用的” Python的文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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