我正试图通过按下按钮来链接2个动作。我正在使用线程 一个函数'callback'打印一条消息,另一个函数创建一个标签 显示gif动画。当我尝试穿线并按下按钮运行时 他们,gif无限展示(不断),但我想拥有它 一旦“回调”完成运行,就从GUI中删除(删除)。任何帮助表示赞赏。
from tkinter import *
from tkinter import ttk
from tkinter import scrolledtext
import threading
import time
root = Tk()
root.title('Title')
def callback():
print('this message')
time.sleep(0.5)
def animate():
while True:
try:
time.sleep(0.04)
img = PhotoImage(file='path',format="gif - {}".format(num))
label1 = ttk.Label(root, image=img)
label1.grid(row=0, column=1)
num+=1
except:
num=0
def thr():
t1= threading.Thread(target=callback)
t2= threading.Thread(target=animate)
t1.start()
t2.start()
button = ttk.Button(root, text='click', command=thr).grid(row=0, column=1, sticky=N)
entry = scrolledtext.ScrolledText(root, width=30, heigh=10, wrap=WORD)
entry.grid(row=0, column=0)
entry.focus()
root.mainloop()
答案 0 :(得分:0)
您可以使用变量通知第二个线程第一个线程结束。
running = False
但它可能不是最好的方法 - 也许您应该使用模块queue
将信息从一个线程发送到另一个线程。
您可以使用label1.destroy()
删除标签和图片,但您只需创建一次标签,之后只能更改此标签中的图片。
label1['image'] = img
代码:
from tkinter import *
from tkinter import ttk
from tkinter import scrolledtext
import threading
import time
#from PIL import Image, ImageTk # for `png`
# --- functions ----
def callback():
# inform function to use external/global variable because you use `=`
global running
print('callback: start')
time.sleep(2)
print('callback: end')
# inform other thread to end running
running = False
def animate():
print('animate: start')
while running:
try:
time.sleep(0.04)
img = PhotoImage(file='path', format="gif - {}".format(num))
#img = Image.open('test/image-{}.png'.format(num)) # for `png`
#img = ImageTk.PhotoImage(img) # for `png`
#label1.img = img # solution for garbage collector problem # for `png`
label1['image'] = img
num += 1
except:
num = 0
#label1.img = None # doesn't work
#label1['image'] = None # doesn't work
# destroy label
label1.destroy()
print('animate: end')
def thr():
# inform function to use external/global variable because you use `=`
global running
global label1
# create new label
label1 = ttk.Label(root)
label1.grid(row=0, column=1)
running = True
t1 = threading.Thread(target=callback)
t2 = threading.Thread(target=animate)
t1.start()
t2.start()
# --- main ---
# create global variables
running = False
label1 = None
# - GUI -
root = Tk()
root.title('Title')
button = ttk.Button(root, text='click', command=thr)
button.grid(row=0, column=1, sticky=N)
entry = scrolledtext.ScrolledText(root, width=30, heigh=10, wrap=WORD)
entry.grid(row=0, column=0)
entry.focus()
root.mainloop()
编辑:您也可以使用
while t1.is_alive():
而不是while running:
编辑:您也可以使用root.after
而不是第二个帖子。
from tkinter import *
from tkinter import ttk
from tkinter import scrolledtext
import threading
import time
#from PIL import Image, ImageTk # for `png`
# --- functions ----
def callback():
# inform function to use external/global variable because you use `=`
global running
print('callback: start')
time.sleep(2)
print('callback: end')
def animate(num=0):
try:
img = PhotoImage(file='path', format="gif - {}".format(num))
#img = Image.open('test/ball-{}.png'.format(num)) # for `png`
#img = ImageTk.PhotoImage(img) # for `png`
#label1.img = img # solution for garbage collector problem # for `png`
label1['image'] = img
num += 1
except:
num = 0
if t1.is_alive():
# execute again after 4ms
root.after(4, animate, num) # 4ms = 0.04s
else:
# destroy label
label1.destroy()
print("animate: end")
def thr():
# inform function to use external/global variable because you use `=`
global label1
global t1
# create new label
label1 = ttk.Label(root)
label1.grid(row=0, column=1)
t1 = threading.Thread(target=callback)
t1.start()
print("animate: start")
animate(0)
# --- main ---
# create global variables
label1 = None
t1 = None
# - GUI -
root = Tk()
root.title('Title')
button = ttk.Button(root, text='click', command=thr)
button.grid(row=0, column=1, sticky=N)
entry = scrolledtext.ScrolledText(root, width=30, heigh=10, wrap=WORD)
entry.grid(row=0, column=0)
entry.focus()
root.mainloop()