在 Tkinter 中从网络中提取数据时制作加载动画

Making a loading animation when pulling data from the web in Tkinter

我从互联网上提取数据并将其显示在 canvas 上。在获取数据的过程中稍等片刻,我想在 canvas 上显示加载动画或进度条。理论上我设法做到了,它应该在 progressbar 开始提取数据并在提取数据后停止但它不起作用时工作。我该怎么办?

from tkinter import*
from tkinter import ttk
import io
from PIL import Image, ImageTk
import requests
from urllib.request import urlopen


class Root(Tk):
    def __init__(self):
        super().__init__()
        
        self.canvas =Canvas(
        width = 720, height = 1000)
        self.canvas.place(relx=0,rely=0)
        
        self.button = Button(self, text="Get The Posters",
        command=self.get)
        self.button.pack()
        
        self.movie_list=["Tenet","Memento","Inception"]
        self.poster_list=[]
        self.x=0
        self.count=0
        
        
    def get(self):
        
        self.button.destroy()
        self.label=Label(text="loading")
        self.label.place(relx=0.08,rely=0.65)
        self.pb = ttk.Progressbar(orient='horizontal',
        mode='indeterminate',length=480)
        
        if self.count==0:
            self.pb.place(relx=0.08,rely=0.7)
            self.pb.start()
        
        for i in self.movie_list:
            self.url = "http://www.omdbapi.com/?apikey=73a4d84d&t={}".format(i)
            self.response = requests.get(self.url)
            self.data = self.response.json()
            self.pic_url=self.data["Poster"]
            self.my_page = urlopen(self.pic_url)
            self.my_picture = io.BytesIO(self.my_page.read())
            self.pil_img = Image.open(self.my_picture).resize((200,296))
            self.tk_img = ImageTk.PhotoImage(self.pil_img)
            self.poster_list.append(self.tk_img)
            self.count=1
            
        if self.count==1:
            self.pb.stop()
            
        for n in range(len(self.poster_list)):
            self.x+=30
            if n==1:
                self.x+=200
            if n==2:
                self.x+=200
            self.canvas.create_image(
            self.x,100,image=self.poster_list[n],anchor=NW)

root = Root()
root.mainloop()

程序不运行的原因是函数运行顺序不对。当我 运行 函数同时使用 'threading'.

时它起作用了
from tkinter import*
from tkinter import ttk
import io
from PIL import Image, ImageTk
import requests,threading
from urllib.request import urlopen


class Root(Tk):
    def __init__(self):
        super().__init__()
        
        self.canvas =Canvas(
        width = 720, height = 1000)
        self.canvas.place(relx=0,rely=0)
        
        self.button = Button(self, text="Get The Posters",
        command=self.play)
        self.button.pack()
        
        self.movie_list=["Tenet","Memento","Inception"]
        self.poster_list=[]
        self.x=0
        self.label=Label(text="loading...please wait...")
        self.pb = ttk.Progressbar(orient='horizontal',mode='indeterminate',length=480)
        
    def animation(self):
        self.button.destroy()
        self.label.place(relx=0.5,rely=0.85,anchor='c')
        self.pb.place(relx=0.5,rely=0.9,anchor='c')
        self.pb.start()
        
    def play(self):
        self.t1 = threading.Thread(target=self.animation)
        self.t2 = threading.Thread(target=self.get_posters)
        self.t1.start()
        self.t2.start()
            
    def get_posters(self):
        
        for i in self.movie_list:
            self.url = "http://www.omdbapi.com/?apikey=73a4d84d&t={}".format(i)
            self.response = requests.get(self.url)
            self.data = self.response.json()
            self.pic_url=self.data["Poster"]
            self.my_page = urlopen(self.pic_url)
            self.my_picture = io.BytesIO(self.my_page.read())
            self.pil_img = Image.open(self.my_picture).resize((200,296))
            self.tk_img = ImageTk.PhotoImage(self.pil_img)
            self.poster_list.append(self.tk_img)
            
        for n in range(len(self.poster_list)):
            self.x+=30
            if n==1:
                self.x+=200
            if n==2:
                self.x+=200
            self.canvas.create_image(
            self.x,100,image=self.poster_list[n],anchor=NW)
            
        self.label.destroy()
        self.pb.destroy()


root = Root()
root.mainloop()