Cyber Tailor
Cyber Tailor

Reputation: 449

PyGObject: Can't control a progressbar

If I use .set_text() and .set_fraction() methods from module level, all successfully.

But if I do this from function or by sending object to other module, nothing happens.

I use Glade. I wrote a program for 5 minutes. Glade:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
  <requires lib="gtk+" version="3.10"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkProgressBar" id="progressbar1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="fraction">0.0</property>
        <property name="pulse_step">0.10</property>
        <property name="show_text">True</property>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="window2">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkButton" id="button1">
        <property name="label" translatable="yes">button</property>
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
      </object>
    </child>
  </object>
</interface>

Very terrible Python script:

from gi.repository import Gtk
import time

def go(*args):
    progress.set_text("Progress...")
    for did in range(100):
        progress.set_fraction(did / 100)
        time.sleep(0.1)

builder = Gtk.Builder()
builder.add_from_file("test.glade")
win = builder.get_object("window1")
win2 = builder.get_object("window2")
win.connect("destroy", Gtk.main_quit)
progress = win.get_child()
button = win2.get_child()
button.connect("clicked", go)
win.show_all()
win2.show_all()
Gtk.main()

UPD1: My steps:

  1. I'm pressing button
  2. The proggressbar staying in old position
  3. When a loop is completed, the progressbar updates

UPD2: video

Upvotes: 1

Views: 138

Answers (1)

tynn
tynn

Reputation: 39853

You're running your progression loop within the gtk main loop. Therefore you're blocking the main loop and all redrawing is delayed until the loop has completed.

You can see this using the threading module like the following:

button.connect("clicked", lambda *a: threading.Thread(target=go, args=a).start())

This time the progessbar updates work.

Upvotes: 1

Related Questions