python-3.x - 如何使用 gio 和 python3 获取文件的图标
问题描述
我想使用 gtk 获取相应文件的默认图标。
我在python3中尝试了这个方法,但它给了我一个错误
from gi.repository import Gio as gio
from gi.repository import Gtk
import os
def get_icon_filename(filename,size):
#final_filename = "default_icon.png"
final_filename = ""
if os.path.isfile(filename):
# Get the icon name
file = gio.File(filename)
file_info = file.query_info('standard::icon')
file_icon = file_info.get_icon().get_names()[0]
# Get the icon file path
icon_theme = gtk.icon_theme_get_default()
icon_filename = icon_theme.lookup_icon(file_icon, size, 0)
if icon_filename != None:
final_filename = icon_filename.get_filename()
return final_filename
print(get_icon_filename("/home/newtron/Desktop/Gkite_Logo.png",64))
呃
File "geticon", line 11, in get_icon_filename
file = gio.File(filename)
TypeError: GInterface.__init__() takes exactly 0 arguments (1 given)
我在 gio 模块方面没有任何经验。我的代码有问题吗?如果它没有解决此错误的方法是什么。
解决方案
正如我的朋友@stovfl所说,这是因为我在 python2 中使用了旧的 Gi 和 Gtk 代码。但是在新版本的gio.File
属性中,有更多的子属性(类)来指定给出什么样的文件属性,例如
new_for_path
类我在我的代码中使用了它并工作了它。实际上我从另一个 StackOverflow 讨论中得到了这段代码。
当我再次检查它时,它有 python3 代码
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gio as gio
from gi.repository import Gtk as gtk
import os
def get_icon_filename(filename,size):
#final_filename = "default_icon.png"
final_filename = ""
if os.path.isfile(filename):
# Get the icon name
file = gio.File.new_for_path(filename)
file_info = file.query_info('standard::icon',0)
file_icon = file_info.get_icon().get_names()[0]
# Get the icon file path
icon_theme = gtk.IconTheme.get_default()
icon_filename = icon_theme.lookup_icon(file_icon, size, 0)
if icon_filename != None:
final_filename = icon_filename.get_filename()
return final_filename
print(get_icon_filename("/home/newtron/Desktop/counter.desktop",48))
推荐阅读
- java - 如何在视图中显示对象的内容?
- acumatica - Acumatica - 使用不同字段的值填充字段
- java - 我的 AnimationDrawable 对象的 run 方法减慢了我的应用程序,有什么解决方案吗?
- c# - 如何在mailbee LoadMessage的路径中进行连接
- hadoop - HDFS如何在VM上找到主机名端口号
- php - Woocommerce“订单支付”页面 - 显示帐单地址?
- java - 如何设置 Spring 以将 ehcache 与 xml 一起使用?以及如何使用 JCache API 只缓存一个没有密钥的对象?
- php - Symfony 4 - 模板和控制器的全局数据
- elasticsearch - 使用 Elasticsearch 数据源在 Grafana 中进行查询模板
- python - Flask + SQLAlchemy:通过运行 python 脚本加载带有记录的数据库