2016-04-29 61 views
0

我想從2個不同的文件夾中打開圖像並將它們彼此相鄰顯示,還可以使用「Next」按鈕移動到下一對圖像。打開2個文件夾中的圖像並在python中同時顯示

路徑存儲在一個txt文件中的圖像,因此,可以說開了第一圖像和第二圖像,當我點擊下一步,第三和第四圖像等

我是新來的蟒蛇,這是我發現,到目前爲止,讀取圖像

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個圖像,並添加一個按鈕

+0

你的問題差不多寬泛;但是,由於您似乎已經知道如何顯示一張圖片,因此您可以確保同時顯示2個圖標(只需添加其他標籤即可像第一張圖片一樣保存圖像,並且可以立即從文本文件中讀取兩行) –

+0

你試過簡單地複製'root.mainloop()'之前的三行來看看會發生什麼? –

回答

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() 
相關問題