2020-06-20圖片V1

import os,time,tkinter,random
from tkinter import filedialog,messagebox
from PIL import Image,ImageTk



##yincangchuangk.withdraw()

relist = []
yincangchuangk = tkinter.Tk()
win_width = yincangchuangk.winfo_screenwidth()
win_height = yincangchuangk.winfo_screenheight()-90
def get_dir():
    #文件位置
    '''
    dirlt = []
    lt = os.listdir(path)
    for i in lt:
        if os.path.isdir(path+'/'+i):
            lt1 = os.listdir(path+'/'+i+'/')#最后一個/加與不加無影響
            for ii in lt1:
                dirlt.append(path+'/'+i+'/'+ii)
    print('調(diào)用get_dir函數(shù)')
    return dirlt
    '''
    dirpath = filedialog.askdirectory()
##    exname = input("輸入指定類型文件:")
    exname='jpg'
    dL=[]
    for i in os.walk(dirpath):
        if i[2]:
            for i3 in i[2]:
                dL.append(i[0]+'/'+i3)
    for i2 in dL:
        if exname in i2:
            yield i2
'''
def f_NePic(dirlt):
    apath = random.choice(dirlt)
    dirlt.remove(apath)
    yincangchuangk.title(apath.split('/')[-2:])
    img = Image.open(apath)
    photo = ImageTk.PhotoImage(img)#TK只可以用.gif的圖片
    imglabel = tkinter.Label(yincangchuangk,image=photo)
    imglabel.grid()
    yincangchuangk.update()
    time.sleep(999)
    #定義在函數(shù)里面,函數(shù)跑完,內(nèi)存會被釋放.'''
def f_N():
    print('調(diào)用f_N函數(shù)')
    num_re = 1
    global xButton,win_width,win_height,read_pic_path,Pre_pic,meunbar#說明了內(nèi)存劃分,列表的修改就沒有用GLOBAL
    meunbar.grid_forget()
##    meunbar.grid_remove()一樣
    xButton.destroy()
    apath = random.choice(dirlt)
    Pre_pic = apath
    read_pic_path.append(apath)
    print('路經(jīng)',apath)
    dirlt.remove(apath)
    print('len(dirlt):',len(dirlt))
    img = Image.open(apath)
    width,height = img.size
    if width > win_width or height > win_height:
        if height - win_height > width - win_width :
            num_re = height / win_height
            print("縮?。?,num_re)
    img = img.resize((int(width/num_re),int(height/num_re)),Image.ANTIALIAS)
    yincangchuangk.title(apath)
    photo = ImageTk.PhotoImage(img)
    xButton = tkinter.Button(yincangchuangk,image=photo,command = f_N)
    xButton.grid()
    yincangchuangk.update()
    yincangchuangk.mainloop()


def f_showMeun(event):
    meunbar.post(event.x_root,event.y_root)
    

def f_previous():
    global read_pic_path,xButton,win_width,win_height,Pre_pic
    if not read_pic_path:
        messagebox.showinfo('tips','pre_pic is none')
        return
    path = read_pic_path.pop()
    if Pre_pic==path:
        path = read_pic_path.pop()
    xButton.destroy()
    img = Image.open(path)
    width,height = img.size
    num_re = 1
    if width > win_width or height > win_height:
        if height - win_height > width - win_width :
            num_re = height / win_height
            print("縮?。?,num_re)

    img = img.resize((int(width/num_re),int(height/num_re)),Image.ANTIALIAS)
    yincangchuangk.title(path)
    photo = ImageTk.PhotoImage(img)
    xButton = tkinter.Button(yincangchuangk,image=photo,command = f_N)
    xButton.grid()
    yincangchuangk.update()
    yincangchuangk.mainloop()

    

if __name__=='__main__':
##    path = filedialog.askdirectory()
    dirlt=list(get_dir())
##    while 1
    apath = random.choice(dirlt)
    read_pic_path = []
    read_pic_path.append(apath)
    dirlt.remove(apath)
    print('路經(jīng)',apath)
    print('len(dirlt):',len(dirlt))
    yincangchuangk.title(apath)
    img = Image.open(apath)
    width,height = img.size
    num_re = 1
    if (int(width) > int(win_width)) or (int(height) > int(win_height)):
        if (int(height) - int(win_height)) > (int(width) - int(win_width)) :
            num_re = height / win_height
            print("縮小:",num_re)
    img = img.resize((int(width/num_re),int(height/num_re)),Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(img)
##    imglabel = tkinter.Label(yincangchuangk,image=photo)
##    imglabel.grid(row=0,column=0,columnspan=3)
    xButton = tkinter.Button(yincangchuangk,image=photo,command = f_N)
    xButton.grid()
    meunbar = tkinter.Menu(yincangchuangk,tearoff=0)
    meunbar.add_command(label="previous",command = f_previous)
    yincangchuangk.bind("<Button-3>",f_showMeun)
    yincangchuangk.mainloop()
##    f_NePic(dirlt)
    
    

    
        
    
    
    
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容