您好,我对 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