2016-08-18 78 views
0

所以我正在構建一個可以選擇比較兩張照片的應用程序。我在屏幕左側放置了一個列表框,並在屏幕右側放置了兩張照片。列表框下有三個按鈕,用於將圖片放置在某個標題下(圖1或圖2)。它可以工作,但我只能在Pic 1和Pic 2上都放一張照片 - 如果我選擇一張照片放在Pic 1下,然後選擇另一張照片放在Pic 2下,第一張照片會消失。如何在同一個Tkinter Toplevel()窗口上顯示兩個圖像

爲了更好地理解我的問題,下面是我的應用程序中的照片瀏覽功能。

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變量導致此問題,所以如果任何人都可以提供替代方案,我將不勝感激。

+0

您是否嘗試過把它們在不同的容器,如幀列表做什麼? –

+0

我沒有...我該怎麼做?據我所知,Frame沒有把圖像放入它的選項... – mdenci

回答

0

我使用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] 
+0

它工作得很好,謝謝!我仍然是這方面的初學者,但通過這個例子,我看到了全局變量可能導致的問題,並且我將盡我所能在將來避免它們! – mdenci

相關問題