gpt4 book ai didi

python - tkinter 线程未终止(gif 在无限循环中运行)

转载 作者:行者123 更新时间:2023-12-03 13:09:20 61 4
gpt4 key购买 nike

我正在尝试通过按下按钮来链接 2 个操作。我正在使用线程
一个函数“回调”打印一条消息,另一个函数创建一个标签
显示 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()

最佳答案

您可以使用变量通知第二个线程第一个线程结束。

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()

关于python - tkinter 线程未终止(gif 在无限循环中运行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41536221/

61 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com