打开2个文件夹中的图像并在python中同时显示它们

时间:2016-04-29 12:34:34

标签: python tkinter

我想从2个不同的文件夹中打开图像并将它们彼此相邻显示,还有一个“下一步”按钮,可以移动到下一对图像。

图像路径存储在txt文件中,所以我们打开第一个图像和第二个图像,当我点击下一个,第3个和第4个图像时依此类推

我是python的新手,这是我到目前为止所读到的图像

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


root = Tk()

img = ImageTk.PhotoImage(Image.open("path.ppm"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

但我无法想象如何同时打开2张图像并添加下一个按钮

1 个答案:

答案 0 :(得分:2)

以下是您要问的一个工作示例:

  from tkinter import *

  def UpdateImg ( ):
     global img1, img2
     img1 = PhotoImage(file=ImgFiles[Cur])
     img2 = PhotoImage(file=ImgFiles[Cur+1])

     LblImg1.configure(image = img1, text=ImgFiles[Cur])
     LblImg2.configure(image = img2, text=ImgFiles[Cur+1] )

  def BtnNext( ):
     global Cur
     if Cur < len(ImgFiles)-2:
        Cur = Cur  + 2
        UpdateImg ( )

  def BtnPrev( ):
     global Cur
     if Cur > 1:
        Cur = Cur  - 2
        UpdateImg ( )

  fp = open("ImgFilesSrc.txt", "r")
  ImgFiles = fp.read().split('\n')
  fp.close()

  Cur = 0
  img1 = img2 = ''
  root = Tk()

  #Create the main Frame -----------------------------------------------------------------
  FrmMain = Frame(root)
  LblImg1 = Label(FrmMain, text = "Picture 1",   anchor=W, width=120, bg="light sky blue")
  LblImg2 = Label(FrmMain, text = "Picture 2",   anchor=W, width=120, bg="light sky blue")

  BtnPrev  = Button(FrmMain, text=" <  ", width=10, command=BtnPrev)
  BtnNext  = Button(FrmMain, text="  > ", width=10, command=BtnNext)   

  LblImg1.grid (row=2, rowspan = 3, column=1, columnspan=3);
  LblImg2.grid (row=2, rowspan = 3, column=4, columnspan=3);

  BtnPrev.grid (row=5, column=2);   BtnNext.grid(row=5, column=4)

  FrmMain.pack(side=TOP, fill=X)     
  #--------------------------------------------------------------------------
  UpdateImg ( )
  root.mainloop()