骑自行车狗照片时出现奇怪的错误
Strange bug when cycling dog photos
我有这段代码可以生成狗的照片并可以循环浏览它们。
import tkinter, urllib.request, json, io
from PIL import Image, ImageTk
main = tkinter.Tk()
main.geometry('550x600+800+300')
urllist = []
doglist = []
dognumber = -1
W, H = 500, 460
def resize_image(img):
ratio = min(W/img.width, H/img.height)
return img.resize((int(img.width*ratio), int(img.height*ratio)), Image.ANTIALIAS)
def last_image():
global doglist, dognumber
dognumber -= 1
if dognumber < 0:
dognumber = 0
l.config(image=doglist[dognumber])
def fetch_image():
global doglist, dognumber, urllist
try:
dognumber += 1
l.config(image=doglist[dognumber])
except IndexError:
dognumber -= 1
dogapi = urllib.request.urlopen(f'https://dog.ceo/api/breeds/image/random')
dogjson = dogapi.read()
dogdict = json.loads(dogjson)
url = dogdict['message']
m = urllib.request.urlopen(url)
mpi = resize_image(Image.open(m))
tkimg = ImageTk.PhotoImage(mpi)
doglist.append(tkimg)
urllist.append(url)
dognumber += 1
l.config(image=tkimg)
l.image = tkimg
def save_dogs():
global urllist
w = open('/'.join(__file__.split("\")[:-1]) + '/doglist.txt', 'w')
w.write('\n'.join(urllist))
l = tkinter.Label(main, image=tkinter.PhotoImage(), width=W, height=H)
b = tkinter.Button(main, text='Next Dog', command=fetch_image)
b2 = tkinter.Button(main, text='Last Dog', command=last_image)
b3 = tkinter.Button(main, text='Save Dogs', command=save_dogs)
for _ in range(10):
fetch_image()
dognumber = -1
l.pack()
b.pack()
b2.pack()
b3.pack()
main.mainloop()
它工作正常,除了生成前 10 张照片时。出于某种原因,即使 dognumber 重置为 -1,“Last Dog”按钮第一次工作,然后当您再次点击“Next Dog”时,它会显示完全不同的照片。为什么会这样?
我试过弄乱 dognumber 变量开始的内容,希望它从第一个元素开始,并且(有点不足为奇)没有发生任何不同。
它显示的是最后生成的图像,因为它每次都更改图像配置,hitting last dog 刚刚更新了它。非常简单的解决方案,但花了一段时间才弄清楚...
我有这段代码可以生成狗的照片并可以循环浏览它们。
import tkinter, urllib.request, json, io
from PIL import Image, ImageTk
main = tkinter.Tk()
main.geometry('550x600+800+300')
urllist = []
doglist = []
dognumber = -1
W, H = 500, 460
def resize_image(img):
ratio = min(W/img.width, H/img.height)
return img.resize((int(img.width*ratio), int(img.height*ratio)), Image.ANTIALIAS)
def last_image():
global doglist, dognumber
dognumber -= 1
if dognumber < 0:
dognumber = 0
l.config(image=doglist[dognumber])
def fetch_image():
global doglist, dognumber, urllist
try:
dognumber += 1
l.config(image=doglist[dognumber])
except IndexError:
dognumber -= 1
dogapi = urllib.request.urlopen(f'https://dog.ceo/api/breeds/image/random')
dogjson = dogapi.read()
dogdict = json.loads(dogjson)
url = dogdict['message']
m = urllib.request.urlopen(url)
mpi = resize_image(Image.open(m))
tkimg = ImageTk.PhotoImage(mpi)
doglist.append(tkimg)
urllist.append(url)
dognumber += 1
l.config(image=tkimg)
l.image = tkimg
def save_dogs():
global urllist
w = open('/'.join(__file__.split("\")[:-1]) + '/doglist.txt', 'w')
w.write('\n'.join(urllist))
l = tkinter.Label(main, image=tkinter.PhotoImage(), width=W, height=H)
b = tkinter.Button(main, text='Next Dog', command=fetch_image)
b2 = tkinter.Button(main, text='Last Dog', command=last_image)
b3 = tkinter.Button(main, text='Save Dogs', command=save_dogs)
for _ in range(10):
fetch_image()
dognumber = -1
l.pack()
b.pack()
b2.pack()
b3.pack()
main.mainloop()
它工作正常,除了生成前 10 张照片时。出于某种原因,即使 dognumber 重置为 -1,“Last Dog”按钮第一次工作,然后当您再次点击“Next Dog”时,它会显示完全不同的照片。为什么会这样?
我试过弄乱 dognumber 变量开始的内容,希望它从第一个元素开始,并且(有点不足为奇)没有发生任何不同。
它显示的是最后生成的图像,因为它每次都更改图像配置,hitting last dog 刚刚更新了它。非常简单的解决方案,但花了一段时间才弄清楚...