Python gi.repository.Gio 模块,ThemedIcon() 实例源码
我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用gi.repository.Gio.ThemedIcon()。
def generate_left_Box(self):
left_Box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
remove_icon = Gio.ThemedIcon(name="user-trash-symbolic")
remove_image = Gtk.Image.new_from_gicon(
remove_icon, Gtk.IconSize.BUTTON)
self.remove_button.set_tooltip_text(_("Remove selected accounts"))
self.remove_button.set_image(remove_image)
self.remove_button.set_sensitive(False)
add_icon = Gio.ThemedIcon(name="list-add-symbolic")
add_image = Gtk.Image.new_from_gicon(add_icon, Gtk.IconSize.BUTTON)
self.add_button.set_tooltip_text(_("Add a new account"))
self.add_button.set_image(add_image)
lock_icon = Gio.ThemedIcon(name="changes-prevent-symbolic")
lock_image = Gtk.Image.new_from_gicon(lock_icon, Gtk.IconSize.BUTTON)
self.lock_button.set_tooltip_text(_("Lock the Application"))
self.lock_button.set_image(lock_image)
settings.connect('changed', self.bind_status)
left_Box.add(self.remove_button)
left_Box.add(self.add_button)
left_Box.add(self.lock_button)
return left_Box
def generate_right_Box(self):
count = self.app.db.count()
right_Box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
select_icon = Gio.ThemedIcon(name="object-select-symbolic")
select_image = Gtk.Image.new_from_gicon(
select_icon, Gtk.IconSize.BUTTON)
self.select_button.set_tooltip_text(_("Selection mode"))
self.select_button.set_image(select_image)
search_icon = Gio.ThemedIcon(name="system-search-symbolic")
search_image = Gtk.Image.new_from_gicon(
search_icon, Gtk.IconSize.BUTTON)
self.search_button.set_tooltip_text(_("Search"))
self.search_button.set_image(search_image)
self.search_button.set_visible(count > 0)
self.cancel_button.set_label(_("Cancel"))
right_Box.add(self.search_button)
right_Box.add(self.select_button)
right_Box.add(self.cancel_button)
return right_Box
def get_from_app(app, size=DEFAULT_SIZE):
try:
gicon = app.get_icon()
pixbuf = None
if gicon:
if isinstance(gicon, Gio.ThemedIcon):
return get_from_list(gicon.get_names(), size=size)
elif isinstance(gicon, Gio.FileIcon):
file = app.get_icon().get_file().get_path()
return get_from_file(file, size)
if not pixbuf:
return get_from_name('application-x-executable', size=size)
except Exception, e:
log.error('get_from_app Failed: %s' % e)
return get_from_name(size=size)
def get_default_icon(directory):
"""Use Gio to get the default icon."""
attributes = ["Metadata::custom-icon",
"Metadata::custom-icon-name",
"standard::icon"]
gfile = Gio.File.new_for_path(directory)
ginfo = gfile.query_info("standard::icon,Metadata::*",
Gio.FileQueryInfoFlags.nofollow_SYMLINKS)
for attribute in attributes:
if ginfo.has_attribute(attribute):
attribute_type = ginfo.get_attribute_type(attribute)
if attribute_type == Gio.FileAttributeType.STRING:
value = ginfo.get_attribute_string(attribute)
if value is not None:
return uriparse(value)
elif attribute_type == Gio.FileAttributeType.OBJECT:
# This return a Gio.ThemedIcon object
value = ginfo.get_attribute_object(attribute)
icon_names = value.props.names
if icon_names:
return icon_names[0]
return "folder"
def generate_popover(self, Box):
settings_icon = Gio.ThemedIcon(name="open-menu-symbolic")
settings_image = Gtk.Image.new_from_gicon(
settings_icon, Gtk.IconSize.BUTTON)
self.settings_button.set_tooltip_text(_("Settings"))
self.settings_button.set_image(settings_image)
self.settings_button.connect("clicked", self.toggle_popover)
self.popover = Gtk.Popover.new_from_model(
self.settings_button, self.app.menu)
self.popover.props.width_request = 200
Box.add(self.settings_button)
def generate_header_bar(self):
"""
Generate the header bar Box
"""
self.hb.props.title = _("Add a new account")
left_Box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
right_Box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
cancel_button = Gtk.Button.new_with_label(_("Cancel"))
cancel_button.connect("clicked", self.close_window)
cancel_button.get_style_context().add_class("destructive-action")
left_Box.add(cancel_button)
self.apply_button = Gtk.Button.new_with_label(_("Add"))
self.apply_button.get_style_context().add_class("suggested-action")
self.apply_button.connect("clicked", self.add_account)
self.apply_button.set_sensitive(False)
qr_button = Gtk.Button()
qr_icon = Gio.ThemedIcon(name="qrscanner-symbolic")
qr_image = Gtk.Image.new_from_gicon(qr_icon, Gtk.IconSize.BUTTON)
qr_button.set_tooltip_text(_("Scan a QR code"))
qr_button.set_image(qr_image)
qr_button.connect("clicked", self.on_qr_scan)
right_Box.add(qr_button)
right_Box.add(self.apply_button)
self.hb.pack_start(left_Box)
self.hb.pack_end(right_Box)
self.set_titlebar(self.hb)
def btn_with_icon(icon):
btn = Gtk.Button()
icon = Gio.ThemedIcon(name=icon)
image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON)
btn.add(image)
return btn
def __init__(self, name: str, icon_name: str):
"""Initialize Button class"""
self._name = name
self._icon_name = icon_name
self._icon = Gio.ThemedIcon(name=icon_name)
self._widget = Gtk.Button()
self._widget.add(Gtk.Image.new_from_gicon(self._icon, Gtk.IconSize.BUTTON))
self._widget.set_name(self._name)
def _build_header_bar(self):
"""Setup window headerbar."""
# Header bar
headerbar = Gtk.HeaderBar()
headerbar_container = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
spacing=3)
title = Gtk.Label()
title.set_text(_("Icon Chooser"))
title.get_style_context().add_class("title")
headerbar_container.pack_start(title, False, 0)
subtitle = Gtk.Label()
subtitle.get_style_context().add_class("subtitle")
subtitle_text = ",".join(self._folders)
subtitle.set_text(subtitle_text)
subtitle.set_ellipsize(Pango.EllipsizeMode.END)
subtitle.set_tooltip_text(subtitle_text)
subtitle.props.max_width_chars = 30
headerbar_container.pack_start(subtitle, 0)
headerbar.set_custom_title(headerbar_container)
headerbar.set_show_close_button(False)
# Search Button
self._search_btn = Gtk.ToggleButton()
search_icn = Gio.ThemedIcon(name="system-search-symbolic")
search_img = Gtk.Image.new_from_gicon(search_icn, Gtk.IconSize.BUTTON)
self._search_btn.set_image(search_img)
# Cancel Button
cancel_button = Gtk.Button()
cancel_button.set_label(_("Cancel"))
cancel_button.connect("clicked", self.close_window)
headerbar.pack_start(cancel_button)
headerbar.pack_end(self._search_btn)
self.set_titlebar(headerbar)
def generate_widget(self, item):
""" Generates gtk widget for specified menutitem """
if isinstance(item, Separator) and item.label:
widget = Gtk.Button.new_with_label(item.label)
widget.set_relief(Gtk.ReliefStyle.NONE)
widget.set_name("osd-menu-separator")
return widget
elif isinstance(item, Separator):
widget = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
widget.set_name("osd-menu-separator")
return widget
else:
widget = Gtk.Button.new_with_label(item.label)
widget.set_relief(Gtk.ReliefStyle.NONE)
if hasattr(widget.get_children()[0], "set_xalign"):
widget.get_children()[0].set_xalign(0)
else:
widget.get_children()[0].set_halign(Gtk.Align.START)
if isinstance(item, Submenu):
item.callback = self.show_submenu
label1 = widget.get_children()[0]
label2 = Gtk.Label(_(">>"))
label2.set_property("margin-left", 30)
Box = Gtk.Box(Gtk.Orientation.HORIZONTAL)
widget.remove(label1)
Box.pack_start(label1, True, 1)
Box.pack_start(label2, 1)
widget.add(Box)
widget.set_name("osd-menu-item")
elif item.id is None:
widget.set_name("osd-menu-dummy")
else:
widget.set_name("osd-menu-item")
if isinstance(item.icon, Gio.FileIcon):
icon_file = item.icon.get_file().get_path()
has_colors = True
elif isinstance(item.icon, Gio.ThemedIcon):
icon = Gtk.IconTheme.get_default().choose_icon(
item.icon.get_names(), 64, 0)
icon_file = icon.get_filename() if icon else None
has_colors = True
else:
icon_file, has_colors = find_icon(item.icon, self.PREFER_BW_ICONS)
if icon_file:
icon = MenuIcon(icon_file, has_colors)
label = widget.get_children()[0]
for c in [] + widget.get_children():
widget.remove(c)
Box = Gtk.Box()
Box.pack_start(icon, False, 0)
Box.pack_start(label, 10)
widget.add(Box)
return widget