Window freezes after clicking of button in python GTK3

*爱你&永不变心* 提交于 2019-12-06 05:17:25

Run a main loop iteration from within your loop:

def on_next2_clicked(self,button):
    cmd = "My Command"
    proc = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE)
    while True:
        line = proc.stdout.read(2)
        if not line:
            break
        self.fper = float(line)/100.0
        self.ui.progressbar1.set_fraction(self.fper)
        while Gtk.events_pending():
            Gtk.main_iteration()  # runs the GTK main loop as needed
    print "Done"

You are busy-waiting, not letting the UI main event loop run. Put the loop in a separate thread so the main thread can continue its own event loop.

Edit: Adding example code

import threading

def on_next2_clicked(self,button):
    def my_thread(obj):
        cmd = "My Command"
        proc = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE)
        while True:
                line = proc.stdout.read(2)
                if not line:
                    break
                obj.fper = float(line)/100.0
                obj.ui.progressbar1.set_fraction(obj.fper)
        print "Done"

    threading.Thread(target=my_thread, args=(self,)).start()

The above modification to your function will start a new thread that will run in parallel with your main thread. It will let the main event loop continue while the new thread does the busy waiting.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!