我从互联网上提取数据并将其显示在画布上。在获取数据时过了一点时间,我想在画布上显示加载动画或进度条。从理论上讲,我设法做到了,它应该在进度条开始提取数据并在提取数据后停止但它不起作用时工作。我应该怎么办?
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()