Tkinter:在线程化函数时传递参数



我试图传递一些参数,而线程函数,这是我的代码:

import tkinter as tk
from PIL import ImageTk, Image, ImageGrab
import time
import threading

class Flashing(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.first_label = tk.Label(self, image="", foreground="black")
self.first_label.pack(padx=50, side="left", anchor="w")
self.button = tk.Button(
self,
text="start",
command=threading.Thread(target=self.flash_all, args=[label, img]).start(),
)
self.button.pack()
def flash_all(self, label, img):
for num in range(6):
num += 1
if (num % 2) == 0:
print("{0} is Even".format(num))
time.sleep(1)
label.config(text="one)
if (num % 2) == 1:
print("{0} is Odd".format(num))
time.sleep(1)
self.bip1.play(loops=0)
label.config(text='two')
if num == 6:
time.sleep(1)
self.bip2.play(loops=0)
label.config(text='three')
time.sleep(5)
label.config(image="")

if __name__ == "__main__":
root = tk.Tk()
Flashing(root).pack(side="top", fill="both", expand=True)
root.mainloop()

但是我得到了这个错误

(virt)

我应该做些什么来修复它?

重要:我已经修剪和改变了代码中的一些标签,使其更容易阅读。除了我提到的那个错误外,原来工作得很好。谢谢大家

不使用线程:

import tkinter as tk

class Flashing(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.num = -1
self.timer_numer = None
self.first_label = tk.Label(self, image="", foreground="black")
self.first_label.pack(padx=50, side="left", anchor="w")
self.button = tk.Button(self, text="start", command=self.flash_all)
self.button.pack()
# We make a list over which we will iterate.
self.list_text = ["one", "two","one", "two","one", "two",
"three", "three", "three", "three", "three", "three"]
def flash_all(self):
# Create a timer - run the function once a second.
self.timer_numer = self.after(1000, self.flash_all)
self.num += 1
print(f"{self.num} is {self.list_text[self.num]}")
self.first_label.config(text=self.list_text[self.num])
if self.num >= 11:
self.first_label.config(text="")
# We stop the timer.
self.after_cancel(self.timer_numer)
self.num = -1

if __name__ == "__main__":
root = tk.Tk()
flash = Flashing(root)
flash.pack(side="top", fill="both", expand=True)
root.mainloop()