正如我在上一个问题中提到的,我正在制作一种传送带,它可以从文件夹中拾取图像,并使它们从左向右移动.出现了一个意想不到的问题,我已经在用画布.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 是:当一个土豆从屏幕上消失时,会从数组中选取一个新的图像,然后我打开图像,更改图像,然后将其传送回开始处. 但它不起作用. 我真的不知道为什么,但我认为这可能是因为传送带也使用相同的功能来更换精灵

推荐答案

我try 的是:当一个土豆从屏幕上消失时,一个新的图像 从数组中选取,然后打开图像,更改图像并 把它传送回最初的地方.但它不起作用

你的问题是可以解决的.

  • 注释掉第65行和第66行的Label小部件.它什么都做不了.
  • 在第68行,删除关键字image = potimg,它将在线更新 在98号线.
  • 在第74行,加global potmig.像这样:global potatoes, potimg
  • 在第93行,#canvas.itemconfig(imga, image = potimg)号出口怎么样?
  • 在第98行,添加这个canvas.itemconfig(imga, image = potimg.放 这是在第一个if-else街区外,在canvas.after之前.

代码片段:

#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)
#---------------------------------------------------------------Potato a movement setup--------------------------------------------------------------
global pos
pos = root.winfo_screenwidth()+20
def move_imga():
    global pos
    global potatoes, potimg
    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.itemconfig(imga, image = potimg)
    canvas.after(25, move_imga)
#----------------------------------------------------------Starts the GUI------------------------------------------------------------
move_imga()
animation()
root.mainloop()

输出:

canada.png <--- first start.
['canada-logo.png', 'canada.png', 'canada_1.png', 'canada_2.png', 'card.png', 'Chess_queen.png', 'cricket.png', 'fia_logo.png', 'guyana.png', 'laser.png', 'NOKIA.png', 'p11.png', 'p12.png', 'p13.png', 'p14.png', 'p15.png', 'p16.png', 'p17.png', 'p18.png', 'p19.png', 'p2.png', 'p20.png', 'p21.png', 'p22.png', 'p23.png', 'supra-logo.png', 'supra.png', 'turks_and_caicos.png']

You will see all outputs in below:
fia_logo.png
p19.png
p23.png
supra-logo.png
supra.png
turks_and_caicos.png
p18.png

Python相关问答推荐

将DF中的名称与另一DF拆分并匹配并返回匹配的公司

为什么我的Python代码在if-else声明中的行之前执行if-else声明中的行?

如何获得每个组的时间戳差异?

PyQt5,如何使每个对象的 colored颜色 不同?'

当我try 在django中更新模型时,模型表单数据不可见

使用Python和文件进行模糊输出

判断solve_ivp中的事件

以异步方式填充Pandas 数据帧

mdates定位器在图表中显示不存在的时间间隔

如何从比较函数生成ngroup?

504未连接IB API TWS错误—即使API连接显示已接受''

Seaborn散点图使用多个不同的标记而不是点

高效生成累积式三角矩阵

Polars时间戳同步延迟计算

在MongoDB文档中仅返回数组字段

如何计算Pandas 中具有特定条件的行之间的天差

基于2级列表的Pandas 切片3级多索引

根据两个lambda条件筛选组并根据条件创建新列的最佳方式是什么?

将多行数据循环到嵌套框架中的单行

如何在开始迭代自定义迭代器类时重置索引属性?