2021-05-01图片V2

import os,time,tkinter,random,re
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
apath = ''
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
    '''
    exname='jpg'
    bool_genxin = int(input("genxin? :"))
    if bool_genxin :
        
        dirpath = filedialog.askdirectory()
        #exname = input("输入指定类型文件:")
        
        dL=[]
        for i in os.walk(dirpath):
            if i[2]:
                for i3 in i[2]:
                    temp_dir = i[0]+'/'+i3
                    dL.append(i[0]+'/'+i3)
        with open('18com','wt')as f:
            for ite in dL:
                
                f.write(str(ite))
                f.write('\n')
    with open('18com','r')as f:
        dL=f.readlines()
    for i2 in dL:
        if exname in i2:
            i2 = i2.strip('\n')
            yield i2
##                if exname in temp_dir:
##                    yield temp_dir
            
'''
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,apath,img#说明了内存划分,列表的修改就没有用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.close()
    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)
    if (int(width) > int(win_width)) or (int(height) > int(win_height)):
        num_re = height / win_height
        num_re2 = width / win_width
        num_re = max([num_re,num_re2])
        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,apath,img
    if not read_pic_path:
        messagebox.showinfo('tips','pre_pic is none')
        return
    path = read_pic_path.pop()
    apath = path #没有这句的话只有IF 成立才会出现apath赋值
    if Pre_pic==path:
        path = read_pic_path.pop()
        apath = path
    
    xButton.destroy()
    img.close()
    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)'''
    if (int(width) > int(win_width)) or (int(height) > int(win_height)):
        num_re = height / win_height
        num_re2 = width / win_width
        num_re = max([num_re,num_re2])
        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()

def f_opendir():
    ddir = os.path.dirname(apath)
    ddir=ddir.replace('/',"\\")
    os.system("explorer.exe {}".format(ddir))

if __name__=='__main__':
    
##    path = filedialog.askdirectory()
    dirlt=list(get_dir())
####    while 1
##    with open('cache','wt')as f:
##        f.write(str(dirlt))
##
##    with open('cache','rt')as f:
##        dirlt=f.read()
##        f.close()
##    dirlt = re.findall('E.+?jpg',dirlt)
    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)):
        num_re = height / win_height
        num_re2 = width / win_width
        num_re = max([num_re,num_re2])
        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)
    meunbar.add_command(label="opendir",command=f_opendir)
    yincangchuangk.bind("",f_showMeun)
    yincangchuangk.mainloop()
##    f_NePic(dirlt)
    
    

    
        
    
    
    
    

你可能感兴趣的:(2021-05-01图片V2)