How can I create a GtkImage from a GIcon witha a fallback?












0














I have a Gio.Icon (or GIcon in C, I'm using pygobject). Right now I'm using the following code to create a Gtk.Image from the Gio.Icon:



image = icon and Gtk.Image(gicon=icon, icon_size=Gtk.IconSize.DIALOG, pixel_size=48, use_fallback=True)


The problem is, that the Gio.Icon isn't garanteed to have a valid icon name/path and when it doesn't it shows a broken image icon. I would like to fall back to using a different icon I know exists if the supplied Gio.Icon is invalid. Is there some way to know if the Gio.Icon is invalid, or if the Gtk.Image would show as a broken image?



EDIT



A minimal example:



import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio

win = Gtk.Window()
win.connect('destroy', Gtk.main_quit)

icon = Gio.Icon.new_for_string('gnome-garbage')
fallback_icon = Gio.Icon.new_for_string('folder')
image = Gtk.Image(gicon=icon, icon_size=Gtk.IconSize.DIALOG, pixel_size=48)
win.add(image)

win.show_all()
Gtk.main()









share|improve this question
























  • Try to load an invalid icon and check if that raises an exception. If that's the case, catch it and put your fallback code.
    – liberforce
    Nov 23 '18 at 10:42










  • docs.quantifiedcode.com/python-anti-patterns/readability/…
    – liberforce
    Nov 23 '18 at 10:43










  • It doesn't throw an exception, it just shows a broken image.
    – Thayne
    Nov 23 '18 at 18:35










  • Even with use_fallback=False? Adding an MVCE would help too.
    – liberforce
    Nov 26 '18 at 16:21












  • From the GtkImage documentation: 'If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers.' And origionally I had use_fallback as False, and had the same problem. use_fallback just tells it split the icon name by hyphens and try to find a shorter name for which an icon does exist.
    – Thayne
    Nov 27 '18 at 6:37
















0














I have a Gio.Icon (or GIcon in C, I'm using pygobject). Right now I'm using the following code to create a Gtk.Image from the Gio.Icon:



image = icon and Gtk.Image(gicon=icon, icon_size=Gtk.IconSize.DIALOG, pixel_size=48, use_fallback=True)


The problem is, that the Gio.Icon isn't garanteed to have a valid icon name/path and when it doesn't it shows a broken image icon. I would like to fall back to using a different icon I know exists if the supplied Gio.Icon is invalid. Is there some way to know if the Gio.Icon is invalid, or if the Gtk.Image would show as a broken image?



EDIT



A minimal example:



import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio

win = Gtk.Window()
win.connect('destroy', Gtk.main_quit)

icon = Gio.Icon.new_for_string('gnome-garbage')
fallback_icon = Gio.Icon.new_for_string('folder')
image = Gtk.Image(gicon=icon, icon_size=Gtk.IconSize.DIALOG, pixel_size=48)
win.add(image)

win.show_all()
Gtk.main()









share|improve this question
























  • Try to load an invalid icon and check if that raises an exception. If that's the case, catch it and put your fallback code.
    – liberforce
    Nov 23 '18 at 10:42










  • docs.quantifiedcode.com/python-anti-patterns/readability/…
    – liberforce
    Nov 23 '18 at 10:43










  • It doesn't throw an exception, it just shows a broken image.
    – Thayne
    Nov 23 '18 at 18:35










  • Even with use_fallback=False? Adding an MVCE would help too.
    – liberforce
    Nov 26 '18 at 16:21












  • From the GtkImage documentation: 'If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers.' And origionally I had use_fallback as False, and had the same problem. use_fallback just tells it split the icon name by hyphens and try to find a shorter name for which an icon does exist.
    – Thayne
    Nov 27 '18 at 6:37














0












0








0


1





I have a Gio.Icon (or GIcon in C, I'm using pygobject). Right now I'm using the following code to create a Gtk.Image from the Gio.Icon:



image = icon and Gtk.Image(gicon=icon, icon_size=Gtk.IconSize.DIALOG, pixel_size=48, use_fallback=True)


The problem is, that the Gio.Icon isn't garanteed to have a valid icon name/path and when it doesn't it shows a broken image icon. I would like to fall back to using a different icon I know exists if the supplied Gio.Icon is invalid. Is there some way to know if the Gio.Icon is invalid, or if the Gtk.Image would show as a broken image?



EDIT



A minimal example:



import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio

win = Gtk.Window()
win.connect('destroy', Gtk.main_quit)

icon = Gio.Icon.new_for_string('gnome-garbage')
fallback_icon = Gio.Icon.new_for_string('folder')
image = Gtk.Image(gicon=icon, icon_size=Gtk.IconSize.DIALOG, pixel_size=48)
win.add(image)

win.show_all()
Gtk.main()









share|improve this question















I have a Gio.Icon (or GIcon in C, I'm using pygobject). Right now I'm using the following code to create a Gtk.Image from the Gio.Icon:



image = icon and Gtk.Image(gicon=icon, icon_size=Gtk.IconSize.DIALOG, pixel_size=48, use_fallback=True)


The problem is, that the Gio.Icon isn't garanteed to have a valid icon name/path and when it doesn't it shows a broken image icon. I would like to fall back to using a different icon I know exists if the supplied Gio.Icon is invalid. Is there some way to know if the Gio.Icon is invalid, or if the Gtk.Image would show as a broken image?



EDIT



A minimal example:



import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio

win = Gtk.Window()
win.connect('destroy', Gtk.main_quit)

icon = Gio.Icon.new_for_string('gnome-garbage')
fallback_icon = Gio.Icon.new_for_string('folder')
image = Gtk.Image(gicon=icon, icon_size=Gtk.IconSize.DIALOG, pixel_size=48)
win.add(image)

win.show_all()
Gtk.main()






gtk gtk3 pygobject






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 6:54

























asked Nov 23 '18 at 5:04









Thayne

3,71212245




3,71212245












  • Try to load an invalid icon and check if that raises an exception. If that's the case, catch it and put your fallback code.
    – liberforce
    Nov 23 '18 at 10:42










  • docs.quantifiedcode.com/python-anti-patterns/readability/…
    – liberforce
    Nov 23 '18 at 10:43










  • It doesn't throw an exception, it just shows a broken image.
    – Thayne
    Nov 23 '18 at 18:35










  • Even with use_fallback=False? Adding an MVCE would help too.
    – liberforce
    Nov 26 '18 at 16:21












  • From the GtkImage documentation: 'If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers.' And origionally I had use_fallback as False, and had the same problem. use_fallback just tells it split the icon name by hyphens and try to find a shorter name for which an icon does exist.
    – Thayne
    Nov 27 '18 at 6:37


















  • Try to load an invalid icon and check if that raises an exception. If that's the case, catch it and put your fallback code.
    – liberforce
    Nov 23 '18 at 10:42










  • docs.quantifiedcode.com/python-anti-patterns/readability/…
    – liberforce
    Nov 23 '18 at 10:43










  • It doesn't throw an exception, it just shows a broken image.
    – Thayne
    Nov 23 '18 at 18:35










  • Even with use_fallback=False? Adding an MVCE would help too.
    – liberforce
    Nov 26 '18 at 16:21












  • From the GtkImage documentation: 'If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers.' And origionally I had use_fallback as False, and had the same problem. use_fallback just tells it split the icon name by hyphens and try to find a shorter name for which an icon does exist.
    – Thayne
    Nov 27 '18 at 6:37
















Try to load an invalid icon and check if that raises an exception. If that's the case, catch it and put your fallback code.
– liberforce
Nov 23 '18 at 10:42




Try to load an invalid icon and check if that raises an exception. If that's the case, catch it and put your fallback code.
– liberforce
Nov 23 '18 at 10:42












docs.quantifiedcode.com/python-anti-patterns/readability/…
– liberforce
Nov 23 '18 at 10:43




docs.quantifiedcode.com/python-anti-patterns/readability/…
– liberforce
Nov 23 '18 at 10:43












It doesn't throw an exception, it just shows a broken image.
– Thayne
Nov 23 '18 at 18:35




It doesn't throw an exception, it just shows a broken image.
– Thayne
Nov 23 '18 at 18:35












Even with use_fallback=False? Adding an MVCE would help too.
– liberforce
Nov 26 '18 at 16:21






Even with use_fallback=False? Adding an MVCE would help too.
– liberforce
Nov 26 '18 at 16:21














From the GtkImage documentation: 'If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers.' And origionally I had use_fallback as False, and had the same problem. use_fallback just tells it split the icon name by hyphens and try to find a shorter name for which an icon does exist.
– Thayne
Nov 27 '18 at 6:37




From the GtkImage documentation: 'If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers.' And origionally I had use_fallback as False, and had the same problem. use_fallback just tells it split the icon name by hyphens and try to find a shorter name for which an icon does exist.
– Thayne
Nov 27 '18 at 6:37












1 Answer
1






active

oldest

votes


















1














I found an answer in the GtkImage documentation:




If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers. If you want to handle errors in loading the file yourself, for example by displaying an error message, then load the image with gdk_pixbuf_new_from_file(), then create the GtkImage with gtk_image_new_from_pixbuf().




Although, in my case I actually need to use a GtkIconTheme to get a PixBuf instead of gdk_pixbuf_new_from_file:



import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio

win = Gtk.Window()
win.connect('destroy', Gtk.main_quit)

def load_icon(icon):
info = Gtk.IconTheme.get_default().lookup_by_gicon(icon, 48, Gtk.IconLookupFlags.FORCE_SIZE)
if info:
return info.load_icon()

icon = Gio.Icon.new_for_string('gnome-garbage')
fallback_icon = Gio.Icon.new_for_string('folder')

pixbuf = load_icon(icon) or load_icon(fallback_icon)
image = Gtk.Image.new_from_pixbuf(pixbuf)
win.add(image)

win.show_all()
Gtk.main()





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440898%2fhow-can-i-create-a-gtkimage-from-a-gicon-witha-a-fallback%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I found an answer in the GtkImage documentation:




    If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers. If you want to handle errors in loading the file yourself, for example by displaying an error message, then load the image with gdk_pixbuf_new_from_file(), then create the GtkImage with gtk_image_new_from_pixbuf().




    Although, in my case I actually need to use a GtkIconTheme to get a PixBuf instead of gdk_pixbuf_new_from_file:



    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk, Gio

    win = Gtk.Window()
    win.connect('destroy', Gtk.main_quit)

    def load_icon(icon):
    info = Gtk.IconTheme.get_default().lookup_by_gicon(icon, 48, Gtk.IconLookupFlags.FORCE_SIZE)
    if info:
    return info.load_icon()

    icon = Gio.Icon.new_for_string('gnome-garbage')
    fallback_icon = Gio.Icon.new_for_string('folder')

    pixbuf = load_icon(icon) or load_icon(fallback_icon)
    image = Gtk.Image.new_from_pixbuf(pixbuf)
    win.add(image)

    win.show_all()
    Gtk.main()





    share|improve this answer


























      1














      I found an answer in the GtkImage documentation:




      If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers. If you want to handle errors in loading the file yourself, for example by displaying an error message, then load the image with gdk_pixbuf_new_from_file(), then create the GtkImage with gtk_image_new_from_pixbuf().




      Although, in my case I actually need to use a GtkIconTheme to get a PixBuf instead of gdk_pixbuf_new_from_file:



      import gi
      gi.require_version('Gtk', '3.0')
      from gi.repository import Gtk, Gio

      win = Gtk.Window()
      win.connect('destroy', Gtk.main_quit)

      def load_icon(icon):
      info = Gtk.IconTheme.get_default().lookup_by_gicon(icon, 48, Gtk.IconLookupFlags.FORCE_SIZE)
      if info:
      return info.load_icon()

      icon = Gio.Icon.new_for_string('gnome-garbage')
      fallback_icon = Gio.Icon.new_for_string('folder')

      pixbuf = load_icon(icon) or load_icon(fallback_icon)
      image = Gtk.Image.new_from_pixbuf(pixbuf)
      win.add(image)

      win.show_all()
      Gtk.main()





      share|improve this answer
























        1












        1








        1






        I found an answer in the GtkImage documentation:




        If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers. If you want to handle errors in loading the file yourself, for example by displaying an error message, then load the image with gdk_pixbuf_new_from_file(), then create the GtkImage with gtk_image_new_from_pixbuf().




        Although, in my case I actually need to use a GtkIconTheme to get a PixBuf instead of gdk_pixbuf_new_from_file:



        import gi
        gi.require_version('Gtk', '3.0')
        from gi.repository import Gtk, Gio

        win = Gtk.Window()
        win.connect('destroy', Gtk.main_quit)

        def load_icon(icon):
        info = Gtk.IconTheme.get_default().lookup_by_gicon(icon, 48, Gtk.IconLookupFlags.FORCE_SIZE)
        if info:
        return info.load_icon()

        icon = Gio.Icon.new_for_string('gnome-garbage')
        fallback_icon = Gio.Icon.new_for_string('folder')

        pixbuf = load_icon(icon) or load_icon(fallback_icon)
        image = Gtk.Image.new_from_pixbuf(pixbuf)
        win.add(image)

        win.show_all()
        Gtk.main()





        share|improve this answer












        I found an answer in the GtkImage documentation:




        If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers. If you want to handle errors in loading the file yourself, for example by displaying an error message, then load the image with gdk_pixbuf_new_from_file(), then create the GtkImage with gtk_image_new_from_pixbuf().




        Although, in my case I actually need to use a GtkIconTheme to get a PixBuf instead of gdk_pixbuf_new_from_file:



        import gi
        gi.require_version('Gtk', '3.0')
        from gi.repository import Gtk, Gio

        win = Gtk.Window()
        win.connect('destroy', Gtk.main_quit)

        def load_icon(icon):
        info = Gtk.IconTheme.get_default().lookup_by_gicon(icon, 48, Gtk.IconLookupFlags.FORCE_SIZE)
        if info:
        return info.load_icon()

        icon = Gio.Icon.new_for_string('gnome-garbage')
        fallback_icon = Gio.Icon.new_for_string('folder')

        pixbuf = load_icon(icon) or load_icon(fallback_icon)
        image = Gtk.Image.new_from_pixbuf(pixbuf)
        win.add(image)

        win.show_all()
        Gtk.main()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 7:47









        Thayne

        3,71212245




        3,71212245






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440898%2fhow-can-i-create-a-gtkimage-from-a-gicon-witha-a-fallback%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Trompette piccolo

            Slow SSRS Report in dynamic grouping and multiple parameters

            Simon Yates (cyclisme)