在PyGTK中,如何启动线程并仅在线程终止时继续调用函数

问题描述 投票:0回答:1

阅读了很多有关线程和.join()函数的问题之后,我仍然找不到如何适应基本的pygobject threads example from documentation,使其与我的用例相匹配:

#!/bin/python3

import threading
import time
from gi.repository import GLib, Gtk, GObject

def app_main():
    win = Gtk.Window(default_height=50, default_width=300)
    win.connect("destroy", Gtk.main_quit)

    def update_progess(i):
        progress.pulse()
        progress.set_text(str(i))
        return False

    def example_target():
        for i in range(50):
            GLib.idle_add(update_progess, i)
            time.sleep(0.2)

    def start_actions(self):
        print("do a few thing before thread starts")
        thread = threading.Thread(target=example_target)
        thread.daemon = True
        thread.start()
        print("do other things after thread finished")

    mainBox = Gtk.Box(spacing=20, orientation="vertical")
    win.add(mainBox)
    btn = Gtk.Button(label="start actions")
    btn.connect("clicked", start_actions)
    mainBox.pack_start(btn, False, False, 0)
    progress = Gtk.ProgressBar(show_text=True)
    mainBox.pack_start(progress, False, False, 0)
    win.show_all()


if __name__ == "__main__":
    app_main()
    Gtk.main()

如何在我的线程终止并且不冻结主窗口的情况下如何使此代码打印"do other things after thread finished"

python-3.x multithreading pygtk pygobject
1个回答
0
投票

首先,为了清楚起见,在调用线程的start方法之后线程并未完成。

© www.soinside.com 2019 - 2024. All rights reserved.