我想使用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所说,这是因为我将旧的Gi和Gtk代码用于python2。但是在新版本的gio.File
attribute中,具有更多子属性(类),这些子属性指定了给定的文件属性类型,例如
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))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句