如果我从模块级别使用 .set_text()和 .set_fraction()方法,则所有方法都成功。
但是如果我从函数或通过将对象发送到其他模块来执行此操作,则不会发生任何事情。
我使用Glade。我写了一个程序5分钟。林间空地:
<?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>
非常糟糕的Python脚本:
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:我的步骤:
UPD2: video
答案 0 :(得分:1)
你正在gtk主循环中运行你的进程循环。因此,您将阻止主循环,并且所有重绘都会延迟,直到循环完成。
您可以使用threading
模块查看此内容,如下所示:
button.connect("clicked", lambda *a: threading.Thread(target=go, args=a).start())
这次progessbar更新工作。