简体   繁体   English

在python tkinter中与滚动视图相关的顶级窗口中未显示图像

[英]Images do not get shown in a scroll view associated top-level window in python tkinter

I have been trying to make a top-level window view which collects and shows all the images present in a folder in columns of 10. If the images were more than the allocated size of the window I wanted it to be possible to scroll through the images. 我一直在尝试制作一个顶层窗口视图,该视图收集并显示第10列的文件夹中存在的所有图像。如果图像大于窗口分配的大小,我希望可以滚动浏览图片。 I followed the answer given to Scrollable Toplevel Window (tkinter) to correctly add an image to a canvas and make it possible to scroll through them. 我按照对Scrollable Toplevel Window(tkinter)给出的答案正确地将图像添加到画布上并可以滚动它们。 But, in my case the entire popup window just comes out to be blank. 但是,就我而言,整个弹出窗口只是空白。 Here is the code 这是代码

import tkinter as tk
from tkinter import *
import glob
import os
from PIL import Image, ImageTk

def pop_up_window():
   win = Toplevel()

   vbar = tk.Scrollbar(win, orient = VERTICAL)
   vbar.grid(row = 0, column = 1, sticky = "ns")

   container = tk.Canvas(win, height=300, width=720, scrollregion=(0, 0, 300, 720))
   container.grid(row = 0, column = 0, sticky = "nsew")

   vbar.config(command=container.yview)
   container.config(yscrollcommand=vbar.set)

   path = "D:\\image_collection"
   COLUMNS = 10
   image_count = 0

   for infile in glob.glob(os.path.join(path, '*.jpg')):
      image_count += 1
      r, c = divmod(image_count-1, COLUMNS)
      im = Image.open(infile)
      resized = im.resize((100, 100), Image.ANTIALIAS)
      img_part = ImageTk.PhotoImage(Image.open(infile).resize((100, 100), Image.ANTIALIAS))
      image_in_canvas = container.create_image(r, c, image = img_part)

   win.rowconfigure(0, weight=1)
   win.columnconfigure(0, weight=1)

root = Tk()
button = Button(root, text='Call Pop-up window', command = pop_up_window)
button.place(x = 0, y = 0)
root.mainloop()

What changes should I make? 我应该做些什么改变?

You need to keep a reference to your images or it will be garbage collected by Python. 您需要保留对图像的引用,否则它将被Python垃圾收集。 A simple change can do it: 一个简单的更改就可以做到:

placeholder = []

def pop_up_window():
    ...

    for infile in glob.glob(os.path.join(path, '*.jpg')):
       image_count += 1
       r, c = divmod(image_count-1, COLUMNS)
       im = Image.open(infile)
       img_part = ImageTk.PhotoImage(Image.open(infile).resize((100, 100), Image.ANTIALIAS))
       placeholder.append(img_part)
       image_in_canvas = container.create_image(r, c, image = img_part)

Also I want to point that the create_image method takes two coordinates as args. 我还要指出, create_image方法采用两个坐标作为args。 You are currently creating them as if they are grids, and it won't show up in the alignment you expected. 您当前正在创建它们,就像它们是网格一样,并且不会以您期望的对齐方式显示。

Your code works but it have few issues with it. 您的代码可以运行,但是几乎没有问题。 I fixed all of them or maybe most of them, in case I forgot to notice it. 如果我忘了注意,我会全部或全部修复。

  • When creating an image in a function always create a reference to them as here you have many images so you can create a list to your container . 在函数中创建图像时,请始终为其创建引用,因为这里有很多图像,因此可以为container创建一个列表。

  • To keep updating the scrollregion depending upon the amount of images bind "" to the container with the callback function lambda e: scrollregion=container.bbox('all') . 要根据图像数量不断更新scrollregion ,请使用回调函数lambda e: scrollregion=container.bbox('all')将“”绑定到container lambda e: scrollregion=container.bbox('all')

Here are the changes that I did to your pop_up_window function. 这是我对pop_up_window函数所做的更改。

...

path = "D:\\image_collection"
COLUMNS = 7
container.img_list = []
column = 0
row = 0

for infile in glob.glob(os.path.join(path, '*.jpg')):
    if column >= COLUMNS: 
        column = 0
        row += 1
    im = Image.open(infile).resize((100, 100), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(im)
    container.img_list.append(img)
    container.create_image(column*100+10, row*100+10, image = img, anchor='nw')
    column += 1

container.bind('<Configure>',lambda e:container.configure(scrollregion=container.bbox('all')))

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM