0

I want to change the icon of a button through the constructor method. But it's not happening as I wish. See the following code:

demo.glade:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkImage" id="image1">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="icon_name">network-transmit-receive</property>
    <property name="icon_size">2</property>
  </object>
  <object class="GtkBox" id="box1">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="orientation">vertical</property>
    <child>
      <object class="GtkBox" id="box3">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButtonBox" id="buttonbox1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="orientation">vertical</property>
            <property name="layout_style">start</property>
            <child>
              <object class="GtkButton" id="button2">
                <property name="label" translatable="yes">button1</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="button3">
                <property name="label" translatable="yes">button2</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="button4">
                <property name="label" translatable="yes">button2</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">2</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">True</property>
        <property name="position">0</property>
      </packing>
    </child>
    <child>
      <object class="GtkBox" id="box2">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkStatusbar" id="statusbar1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="valign">center</property>
            <property name="margin_start">10</property>
            <property name="margin_end">10</property>
            <property name="hexpand">True</property>
            <property name="orientation">vertical</property>
            <property name="spacing">2</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkSpinner" id="spinner1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="halign">center</property>
            <property name="valign">center</property>
            <property name="active">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="halign">center</property>
            <property name="valign">center</property>
            <property name="image">image1</property>
            <property name="always_show_image">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">True</property>
        <property name="position">1</property>
      </packing>
    </child>
  </object>
</interface>

demo.py

import gi

gi.require_version('Gtk','3.0')

from gi.repository import Gtk
from others.dbconnections import PgSqlConnection

class MyWind(Gtk.ApplicationWindow):

    def __init__(self, app):
        super().__init__(application=app)
        self.builder = Gtk.Builder.new_from_file('demo.glade')

        self.add(self.builder.get_object('box1'))

        if not PgSQLConnection.test():
            self.builder.get_object('image1').set_from_icon_name('network-error', Gtk.IconSize.SMALL_TOOLBAR)

        self.builder.get_object('spinner1').stop()

The PgSqlConnection.test() is a method that returns True if the connection was successful otherwise it returns False. However, this method takes a few seconds to return. So the window is displayed only after the return of the method. This is not the behavior I want. I want to display the window while waiting for the return of the method test. The component spinner1 is active by default. Depending on the return, the icon of the button1 is changes and spinner1 will be disabled. The spinner1 component is a animation that indicate that something is being processed.

Matheus Saraiva
  • 1,144
  • 1
  • 11
  • 33
  • 1
    if PgSqlConnection.test() takes some time to run and it is called in your GUI thread then it will block until it returns. If you had set an icon before that call and then set another icon after that call then only the second icon will be displayed. –  Nov 06 '16 at 09:46
  • What is the solution to get the effect I want? A parallel thread to call the test method? – Matheus Saraiva Nov 06 '16 at 17:08
  • 1
    Put the call to PgSqlConnection.test() in another thread. And use one of GTKs methods to monitor when it has finished. –  Nov 07 '16 at 06:59

0 Answers0