正如我在上一个问题中提到的,我正在制作一种传送带,它可以从文件夹中拾取图像,并使它们从左向右移动.出现了一个意想不到的问题,我已经在用画布.Image=(新图像)更改图像以设置传送带的动画,我认为我可以对其他对象执行同样的操作:
#Import
import os
import random
from PIL import Image, ImageTk
from tkinter import Tk, Canvas, PhotoImage, NW
from tkinter import Label
# folder path
dir_path = r'C:\Users\Usuario\Downloads\Experiment'
#------------------------------------------------------------Select Random Image--------------------------------------------------------
imgExtension = ["webp","png"] #Image Extensions to be chosen from
allImages = list()
def Makelist(directory):
for img in os.listdir(directory): #Lists all files
ext = img.split(".")[len(img.split(".")) - 1]
if (ext in imgExtension):
allImages.append(img)
return allImages
Makelist(dir_path)
global potatoes
potatoes = allImages
choice = random.randint(0, len(potatoes) - 1)
randomImage = potatoes[choice] #Image chosen
print(randomImage)
potatoes.remove(randomImage)
#------------------------------------------------------------Canvas---------------------------------------------------------------
root = Tk()
root.attributes('-transparentcolor','#f0f0f0')
canvas = Canvas(root, width = root.winfo_screenwidth(), height=300)
canvas.pack()
#-------------------------------------------------------Conveyor belt gif------------------------------------------------------------
file = r'C:\Users\Usuario\Downloads\Python\Cinta'
allImages = []
Makelist(file)
conveyor = allImages
global current_con
current_con = 0
print(conveyor)
conveyori = Image.open(file+"/"+conveyor[current_con])
conveyorimg = ImageTk.PhotoImage(conveyori)
conveyor_canva = canvas.create_image(0, 0, anchor=NW, image = conveyorimg)
conveyor_label = Label(canvas, image = conveyorimg)
conveyor_label = conveyorimg
def animation():
global current_con
if (current_con < 3):
current_con += 1
else:
current_con = 0
conveyori = Image.open(r'C:/Users/Usuario/Downloads/Python/Cinta/'+conveyor[current_con])
conveyorimg = ImageTk.PhotoImage(conveyori)
canvas.itemconfig(conveyor_canva, image = conveyorimg)
canvas.image = conveyorimg
canvas.after(105, animation)
#-------------------------------------------------------------Potato a image setup------------------------------------------------------------------
im_O = Image.open(r'C:/Users/Usuario/Downloads/Experiment/'+ randomImage)
new_image = im_O.resize((100, 100))
potimg = ImageTk.PhotoImage(new_image)
potato_label = Label(canvas, image = potimg)
potato_label = potimg
#-----------------------------------------------------Positioning the Image inside the canvas-------------------------------------------------------
imga = canvas.create_image(root.winfo_screenwidth()+20, 100, anchor=NW, image = potimg)
#---------------------------------------------------------------Potato a movement setup--------------------------------------------------------------
global pos
pos = root.winfo_screenwidth()+20
def move_imga():
global pos
global potatoes
pos -= 5
# move the image
canvas.move(imga, -5, 0)
if (pos < -90):
#Potato change
if (potatoes == []): #If there are no images left make a new list
allImages = []
Makelist(dir_path)
potatoes = allImages
#Ok now let's actually change the image
choice = random.randint(0, len(potatoes) - 1)
randomImage = potatoes[choice] #Image chosen
print(randomImage)
potatoes.remove(randomImage)
#Do all the stuff, resize it, etc
im_O = Image.open(r'C:/Users/Usuario/Downloads/Experiment/'+ randomImage)
new_image = im_O.resize((100, 100))
potimg = ImageTk.PhotoImage(new_image)
canvas.itemconfig(imga, image = potimg)
canvas.image = potimg
canvas.moveto(imga, root.winfo_screenwidth()+20, 100)
pos = root.winfo_screenwidth()+20
# move again after 25 ms (0.025s)
canvas.after(25, move_imga)
#----------------------------------------------------------Starts the GUI------------------------------------------------------------
move_imga()
animation()
root.mainloop()
我的try 是:当一个土豆从屏幕上消失时,会从数组中选取一个新的图像,然后我打开图像,更改图像,然后将其传送回开始处. 但它不起作用. 我真的不知道为什么,但我认为这可能是因为传送带也使用相同的功能来更换精灵