0

In my Tkinter program I would like to display the last 3 pictures of a folder. However, these are not displayed when running. Here is the code:

        pics = glob.glob('path/to/folder/*.png')
        pics = (sorted(pics, key=os.path.getmtime))
        pics = pics[::-1]
        bildertimer = 3
        bild = 0
        x_koo = 0
        y_koo = 0
        bilderraum = Canvas(width=1750, height=930)
        bilderraum.place(x=130, y=20)
        while bildertimer > 0:
            if x_koo > 1430:
                x_koo = 0
                y_koo = y_koo + 200
            img = PhotoImage(file=pics[bild])
            img.image = pics[bild]
            img_small = img.subsample(6)
            bilderraum.create_image(x_koo, y_koo, anchor='nw', image=img_small)
            bildertimer = bildertimer - 1
            bild = bild + 1
            x_koo = x_koo + 340

If I change the 3 in the bildertimer to 4, the last one is shown to me, but only where the 2nd picture should be. In addition, I then have the list index out of range error, which means the program does not continue. What am I doing wrong?

OFS12
  • 1
  • 1
  • I know it's not the cleanest and most professional code, whoever has a better solution can tell me! – OFS12 Aug 30 '20 at 16:25
  • You should post in English in Stackoverflow. Otherwise, the question will be flagged as "Low Quality" (see https://meta.stackoverflow.com/questions/284552/what-flag-for-a-non-english-answer-or-question) – David Duran Aug 30 '20 at 16:29
  • Its English now. – OFS12 Aug 30 '20 at 16:30
  • You will need to associate the images with the widget, so that it is not garbage collected. Use this command `bilderraum.image = img_small` after you have used the `bilderraum.create_image()` command. – astqx Aug 30 '20 at 16:55
  • You can also check this out for more information http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm – astqx Aug 30 '20 at 17:05
  • Is this code inside a function? – Bryan Oakley Aug 30 '20 at 17:12
  • Yes, ist is inside a function. – OFS12 Aug 30 '20 at 20:28
  • @AST Now the picture is already displayed at bildertimer = 3, but only the last one. Where are the other pictures? Will they be deleted if I redefine the PhotoImage? – OFS12 Aug 31 '20 at 14:03

0 Answers0