因此,我正在构建一个可以比较两张照片的应用程序。我在屏幕左侧放置了一个列表框,在屏幕右侧放置了两张照片的位置。列表框下面有三个按钮,用于将图片放置在特定标题下(图片1或图片2)。它可以工作,但是我只能在Pic 1和Pic 2上都放一张照片-如果我选择一张照片并将其放在Pic 1下,然后选择另一张照片并将其放在Pic 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
变量会导致此问题,因此,如果有人可以提供替代方法,我将不胜感激。
我正在使用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]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句