如何更新画布上的图像?

如何更新画布上的图像?,第1张

如何更新画布上的图像

Add image to canvas:

self.image_on_canvas = self.canvas.create_image(0, 0, image = ...)

Change image:

self.canvas.itemconfig(self.image_on_canvas, image = ...)


Full example:

from Tkinter import *#----------------------------------------------------------------------class MainWindow():    #----------------    def __init__(self, main):        # canvas for image        self.canvas = Canvas(main, width=60, height=60)        self.canvas.grid(row=0, column=0)        # images        self.my_images = []        self.my_images.append(PhotoImage(file = "ball1.gif"))        self.my_images.append(PhotoImage(file = "ball2.gif"))        self.my_images.append(PhotoImage(file = "ball3.gif"))        self.my_image_number = 0        # set first image on canvas        self.image_on_canvas = self.canvas.create_image(0, 0, anchor = NW, image = self.my_images[self.my_image_number])        # button to change image        self.button = Button(main, text="Change", command=self.onButton)        self.button.grid(row=1, column=0)    #----------------    def onButton(self):        # next image        self.my_image_number += 1        # return to first image        if self.my_image_number == len(self.my_images): self.my_image_number = 0        # change image        self.canvas.itemconfig(self.image_on_canvas, image = self.my_images[self.my_image_number])#----------------------------------------------------------------------root = Tk()MainWindow(root)root.mainloop()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5620206.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存