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('调用get_dir函数')
    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)
    #定义在函数里面,函数跑完,内存会被释放.'''
def f_N():
    print('调用f_N函数')
    num_re = 1
    global xButton,win_width,win_height,read_pic_path,Pre_pic,meunbar#说明了内存划分,列表的修改就没有用GLOBAL
    meunbar.grid_forget()
##    meunbar.grid_remove()一样
    xButton.destroy()
    apath = random.choice(dirlt)
    Pre_pic = apath
    read_pic_path.append(apath)
    print('路经',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('路经',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)
    
    

    
        
    
    
    
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。