如何在同一个Tkinter Toplevel()窗口上显示两个图像

时间:2016-08-18 08:16:42

标签: python image tkinter listbox global-variables

所以我正在构建一个可以比较两张照片的应用程序。我在屏幕的左侧放置了一个列表框,并在屏幕的右侧放置了两张照片的位置。列表框下方有三个按钮,用于在特定标题下放置图片(图片1或图片2)。它可以工作,但我只能在Pic 1和Pic 2上放一张照片 - 如果我选择一张照片并将其放在图片1下,然后选择另一张照片并将其放在图片2下,第一张照片会消失。

为了更好地理解我的问题,这是我的应用程序中的照片浏览功能。

from Tkinter import *
import tkMessageBox
from PIL import ImageTk, Image

def photo_browse ():

    def add_photo (screen, column_num):

        if not "pic" in globals ():
            tkMessageBox.showerror ("Error!", "No picture selected!")
            screen.lift ()
        else:
            screen.image = pic
            can = Label (screen, image = pic)
            can.grid (row = 0, column = column_num)
            Label (screen, text = chosen_photo).grid (row = 1, column = column_num)

    def selection_listbox (evt):

        global chosen_photo
        chosen_photo = str (photo_listbox.get (photo_listbox.curselection ()))
        image_location = "Pics/" + chosen_photo
        global pict
        pict = Image.open (image_location)
        global pic
        pic = ImageTk.PhotoImage (pict)

    import glob

    photo_browse_screen = Toplevel ()
    photo_browse_screen.title ("Photo browse")
    photo_browse_screen.geometry ("1000x600")
    photo_browse_screen.resizable (0, 0)
    photo_listbox = Listbox (photo_browse_screen, width = 50, height = 35)
    photo_listbox.grid (columnspan = 3)
    photo_listbox.bind ('<<ListboxSelect>>', selection_listbox)
    name_list = glob.glob ("Pics/*.jpg")
    name_list = [i [6:] for i in name_list]
    name_list.sort ()
    n = 1
    m = 0
    for i in name_list:
        photo_listbox.insert (n, name_list [m])
        n += 1
        m += 1
    Button (photo_browse_screen, text = "PIC 1", command = lambda: add_photo (photo_browse_screen, 4)).grid (row = 1, column = 0)
    Button (photo_browse_screen, text = "PIC 2", command = lambda: add_photo (photo_browse_screen, 5)).grid (row = 1, column = 1)
    Button (photo_browse_screen, text = "EXIT", command = photo_browse_screen.destroy).grid (row = 1, column = 2)
    can_pic_1 = Label (photo_browse_screen, text = "Pic 1", font= "-weight bold")
    can_pic_1.grid (row = 0, column = 4, padx = (200, 100), sticky = N)
    can_pic_2 = Label (photo_browse_screen, text = "Pic 2", font= "-weight bold")
    can_pic_2.grid (row = 0, column = 5, padx = (100, 150), sticky = N)

root = Tk ()
root.title ("Main menu")
root.geometry ("1000x600")
root.resizable (0, 0)
main_menu = Menu (root)

photos_menu = Menu (main_menu, tearoff = 0)

main_menu.add_cascade (label = "Photos", menu = photos_menu)
photos_menu.add_command (label = "Browse photos", command = photo_browse)

root.config (menu = main_menu)

root.mainloop ()

在上一个功能(添加照片)中,我将照片尺寸调整为308x440尺寸,以便它们完美地适合此窗口。

在审核了我的代码后,我几乎肯定global变量导致了这个问题,所以如果有人可以提供替代方案,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

我正在使用python 3.4所以我不是100%确定这对你有用(对我来说Tkinter是tkinter)

当我想要使用多个图像时,我将它们全部添加到列表中,之后您无需对列表执行任何操作

这对我来说一直都有用,我在没有列表的情况下遇到了同样的问题

from Tkinter import *
import tkMessageBox
from PIL import ImageTk, Image

global all_images
all_images = []

def photo_browse ():
    def selection_listbox (evt):    
        global chosen_photo
        chosen_photo = str (photo_listbox.get (photo_listbox.curselection ()))
        image_location = "Pics/" + chosen_photo
        global pict
        pict = Image.open (image_location)
        global pic
        pic = ImageTk.PhotoImage (pict)

        # adding all the images to a list has worked for me in the past
        global all_images
        all_images = all_images + [chosen_photo, pic, pict]