我想使用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))
Err
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))