Tkinter没有显示任何图像

时间:2014-08-06 01:54:18

标签: python image tkinter

这是我的问题,当我运行以下代码时会出现一个非常小的窗口,如果我调整它的大小,我可以看到正确的图像名称循环但没有图像。我知道我需要保存图像对象的引用以防止它被垃圾收集,但我似乎无法弄清楚如何根据情况做到这一点。

from itertools import cycle
try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk
class App(tk.Tk):
    '''Tk window/label adjusts to size of image'''
    def __init__(self, image_files, x, y, delay):
        # the root will be self
        tk.Tk.__init__(self)
        # set x, y position only
        self.geometry('+{}+{}'.format(x, y))
        self.delay = delay
        # allows repeat cycling through the pictures
        # store as (img_object, img_name) tuple
        self.pictures = cycle((tk.PhotoImage(image), image)for image in image_files)
        self.picture_display = tk.Label(self)
        self.picture_display.pack()
    def show_slides(self):
        '''cycle through the images and show them'''
        # next works with Python26 or higher
        img_object, img_name = next(self.pictures)
        self.picture_display.config(image=img_object)
        # shows the image filename, but could be expanded
        # to show an associated description of the image
        self.title(img_name)
        self.after(self.delay, self.show_slides)
    def run(self):
        self.mainloop()
# set milliseconds time between slides
delay = 3500
# get a series of gif images you have in the working folder
# or use full path, or set directory to where the images are
image_files = []
for number in range(1,10):
    image_files.append("DPS%05d.jpx" % number)
# upper left corner coordinates of app window
x = 100
y = 50
app = App(image_files, x, y, delay)
app.show_slides()
app.run()

1 个答案:

答案 0 :(得分:0)

我认为这会奏效,但我并不确定。请记得拥有.gif图片

from itertools import cycle
try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk
class App(tk.Tk):
    '''Tk window/label adjusts to size of image'''
    def __init__(self, image_files, x, y, delay):
        # the root will be self
        tk.Tk.__init__(self)
        # set x, y position only
        self.geometry('+{}+{}'.format(x, y))
        self.delay = delay
        # allows repeat cycling through the pictures
        # store as (img_object, img_name) tuple
        for image in image_files:
            self.pictures = ((tk.PhotoImage(image), image))

        self.picture_display = tk.Label(image = self.pictures)
        self.picture_display.pack()
    def show_slides(self):
        '''cycle through the images and show them'''
        # next works with Python26 or higher
        self.picture_display.config(image=self.pictures)
        # shows the image filename, but could be expanded
        # to show an associated description of the image
        self.after(self.delay, self.show_slides)
    def run(self):
        self.mainloop()
# set milliseconds time between slides
delay = 3500
# get a series of gif images you have in the working folder
# or use full path, or set directory to where the images are
image_files = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for x in list(image_files):
    image_files.append("DPS%05d.gif" % x)
# upper left corner coordinates of app window
x = 100
y = 50
app = App(image_files, x, y, delay)
app.show_slides()
app.run()

这只是黑暗中的事情,但它可能会起作用(我希望如此),只有6次观看,我认为任何可能都有帮助。