【问题标题】:python threading and progressbarspython线程和进度条
【发布时间】:2013-06-15 13:36:38
【问题描述】:

您好,我对 python 比较陌生,我有一些关于我试图编写的脚本的问题(如下)。 `

import Tkinter as tk
import ttk
import time
import threading
def foo():
    time.sleep(5) # simulate some work
def start_foo_thread():
    global foo_thread
    foo_thread = threading.Thread(target=foo)
    foo_thread.daemon = True
    print("it ran")
    stepprogress.start()
    foo_thread.start()
    root.after(20, check_foo_thread)

def check_foo_thread():
    if foo_thread.is_alive():
        root.after(20, check_foo_thread)
    else:
        stepprogress.stop()
def cancel():
    print("cancelling")
def backround():
    print("running in background")
root = tk.Tk()
frame = tk.Frame(root)
frame.grid()
tk.Label( frame, text="importing").grid(row=1, column=1, columnspan=2)
tk.Label( frame, text="starting").grid(row=2, column=1, columnspan=2)
stepprogress = ttk.Progressbar(frame, orient="horizontal", length=200, mode="determinate").grid(row=3, column=1, columnspan=2)
tk.Label( frame, text="overall progress").grid(row=4, column=1, columnspan=2)
mainprogress = ttk.Progressbar(frame, orient="horizontal", length=200, mode="determinate").grid(row=5, column=1, columnspan=2)
tk.Button(frame, text="Cancel", command=cancel).grid(row=6, column=1)
tk.Button(frame, text="run in background", command=backround).grid(row=6, column=2)
frame.pack()
start_foo_thread()
root.mainloop()

`

以下是我的问题。

1.no matter what I do I always get "nonetype has no attribute start?
2.also I was wondering about how I could safely close the thread and the window when someone hits cancel?
3.I also wondered how I could make it that when someone hits the "background" button the window could close, but not the thread?
4.Is it possible for me to have the process to be threaded in a different file, how so?

我更像是一个html/javascript 编码器,而不是一个 python 程序员。

提前致谢, 约翰·S

【问题讨论】:

    标签: python multithreading progress-bar ttk nonetype


    【解决方案1】:

    1) 这是因为您将 grid 方法的返回 None 分配给 stepprogress;你可以这样修复它:

    stepprogress = ttk.Progressbar(frame, orient="horizontal", length=200, mode="determinate")
    stepprogress.grid(row=3, column=1, columnspan=2)
    

    2) 你可以使用 flag 来控制线程的执行,然后等待它正确结束后再退出:

    def foo():
        global load
        i = 1
        while load and i <= 10:
            print("running...")
            time.sleep(1) # simulate some work
            i = i + 1
        print("end of thread")
    ...
    def cancel():
        global load
        load = False
        while foo_thread.is_alive():
            print("waiting thread...")
            time.sleep(0.1)
        print("exiting...")
        root.destroy()
    

    3) 你可以使用 root.withdraw() 来隐藏窗口:

    def backround():
        print("running in background")
        root.withdraw()
    

    这里是一个总结的例子:

    import tkinter as tk
    import ttk
    import time
    import threading
    def foo():
        i = 1
        while load and i <= 10:
            print("running...")
            time.sleep(1) # simulate some work
            i = i + 1
        print("end of thread")
    def start_foo_thread():
        global foo_thread
        foo_thread = threading.Thread(target=foo)
        foo_thread.daemon = True
        print("it ran")
        stepprogress.start()
        foo_thread.start()
        root.after(20, check_foo_thread)
    
    def check_foo_thread():
        if foo_thread.is_alive():
            root.after(20, check_foo_thread)
        else:
            stepprogress.stop()
    def cancel():
        global load
        load = False
        while foo_thread.is_alive():
            print("waiting thread...")
            time.sleep(0.1)
        print("exiting...")
        root.destroy()
    def backround():
        print("running in background")
        root.withdraw()
    root = tk.Tk()
    frame = tk.Frame(root)
    frame.grid()
    load = True
    tk.Label( frame, text="importing").grid(row=1, column=1, columnspan=2)
    tk.Label( frame, text="starting").grid(row=2, column=1, columnspan=2)
    stepprogress = ttk.Progressbar(frame, orient="horizontal", length=200, mode="determinate")
    stepprogress.grid(row=3, column=1, columnspan=2)
    tk.Label( frame, text="overall progress").grid(row=4, column=1, columnspan=2)
    mainprogress = ttk.Progressbar(frame, orient="horizontal", length=200, mode="determinate").grid(row=5, column=1, columnspan=2)
    tk.Button(frame, text="Cancel", command=cancel).grid(row=6, column=1)
    tk.Button(frame, text="run in background", command=backround).grid(row=6, column=2)
    frame.pack()
    start_foo_thread()
    root.mainloop()
    

    4)“让进程在不同的文件中线程化”是什么意思,将线程函数隔离在另一个模块中?...

    【讨论】:

      猜你喜欢
      • 2014-01-06
      • 1970-01-01
      • 2011-06-21
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      相关资源
      最近更新 更多