zoukankan      html  css  js  c++  java
  • 照片查看器2.0

    照片查看器2.0

    import tkinter as tk
    import tkinter.messagebox as msg
    from tkinter.filedialog import askdirectory
    import os, time
    from PIL import ImageTk, Image
    
    
    def select_path():
        global file_path, pic_list
        path_ = askdirectory()
        path.set(path_)
        file_path = path_ + "/"
        # print(file_path)
        pic_list = get_file(file_path)
        btn_first_click()
    
    
    def show_pic_info():
        msg_title = "图片信息"
        info = "图片名称:{}
    路径:{}
    
    原始尺寸大小:{}*{}
    显示尺寸大小:{}*{}" 
            .format(title, full_name, w, h, img_new_w, img_new_h)
        msg.showinfo(msg_title, info)
    
    
    def help_info():
        msg_title = "帮助"
        info = """
        {}
    
        michael照片查看器:
    
        用于查看jpg、jpeg图片,占时不支持查看gif格式图片
    
        版本:{}
    
        制作者:{}
    
        联系邮箱:{}
    
        {}
        """.format("-" * 50, version, author, email, "-" * 50)
        msg.showinfo(msg_title, info)
    
    
    def resize_img(img):
        global root
        root.update()
        root_w = root.winfo_width()
        root_h = root.winfo_height()
        root_h = root_h - 120
        print(root_w, root_h)
        w, h = img.size
        if h >= root_h:
            scale = h / root_h
            w = int(w / scale)
            h = root_h
        img = img.resize((w, h), Image.ANTIALIAS)
        return (img, w, h)
    
    
    def show_pic(title, full_name):
        global i, total, img, img_new_w, img_new_h, w, h
        total = len(pic_list)
        # 图片标题
        print(title)
        title = title + "	" + str(i + 1) + "/" + str(total)
        lbl_pic_title.configure(text=title)
        # 图片
        print(full_name)
        img = Image.open(full_name)
        w, h = img.size
        img, img_new_w, img_new_h = resize_img(img)
        photo = ImageTk.PhotoImage(img)
        lbl_pic.configure(image=photo)
        lbl_pic.image = photo
    
    
    def btn_auto_play_click():
        global i, title, full_name, auto_play, flag
    
        if flag % 2 == 1:
            auto_play = True
            btn_auto_play.configure(text="停止播放")
            flag += 1
        else:
            auto_play = False
            btn_auto_play.configure(text="自动播放")
            flag += 1
    
        print(flag)
        while auto_play:
            if time.localtime().tm_sec % 2 == 0:  # time.localtime().tm_sec,本地时间 秒
                if i < len(pic_list) - 1:
                    i += 1
                title, full_name = pic_list[i]
                show_pic(title, full_name)
                time.sleep(2)
    
    
    def btn_first_click():
        global i, title, full_name, auto_play
        auto_play = False
    
        i = 0
        title, full_name = pic_list[i]
        show_pic(title, full_name)
    
    
    def btn_pre_click():
        global i, title, full_name, auto_play
        auto_play = False
        if i <= 0:
            i = 0
        else:
            i = i - 1
        title, full_name = pic_list[i]
        show_pic(title, full_name)
    
    
    def btn_next_click():
        global i, title, full_name, auto_play
        auto_play = False
        if i < len(pic_list) - 1:
            i += 1
        title, full_name = pic_list[i]
        show_pic(title, full_name)
    
    
    def btn_last_click():
        global i, title, full_name, auto_play
        auto_play = False
        i = len(pic_list) - 1
        title, full_name = pic_list[i]
        show_pic(title, full_name)
    
    
    def get_file(file_path):
        """
    
        :return: 返回一个元组(文件名不带扩展,文件绝对路径)
        """
        pic_list = []
        try:
            lists = os.listdir(file_path)
            for item in lists:
                if (item.split(".")[1] == "jpg"):
                    full_name = file_path + item  # D:pypycharm	k_project/pic/0.jpg
                    title = item.split(".")[0]  # 去掉.jpg后缀 #小花狗(item是小花狗.jpg)
                    pic_list.append((title, full_name))
        except Exception as e:
            print(e)
            print("无此路径")
        return pic_list
    
    
    def main():
        global i, pic_list, file_path, title, full_name, lbl_pic, lbl_pic_title, frame_content, root_w, root_h, root, img, 
            version, author, email, path, auto_play, btn_auto_play, flag
        version = "V2.1_20100517"
        author = "yxmichael"
        email = "yxmichael@gmail.com"
        auto_play = True
        flag = 1
    
        i = 0
        file_path = os.getcwd() + "/"
        pic_list = get_file(file_path)
    
        # 根窗口
        root = tk.Tk()
        root.title("michael照片查看器")
        root.geometry("600x400")
        root.update()  # 刷新窗口,这一点很重要,如果设置完宽高不进行刷新,获取的数据是不准确的
        root_w = root.winfo_width()
        root_h = root.winfo_height()
        # print(root.winfo_width())  #返回tk窗口对象的宽度
        # print(root.winfo_height())  #返回tk窗口对象的高度
        path = tk.StringVar()  # 创建一个字符串跟踪变量
        path.set("D:/py/pycharm/tk_project/pic/")
    
        # 功能栏-顶部
        frame_menu_bar = tk.Frame(root, bg="orange")
        tk.Button(frame_menu_bar, text="打开", command=select_path).pack(side="left", padx=5, ipadx=5)
        entry_path = tk.Entry(frame_menu_bar, textvariable=path, width=60)
        entry_path.pack(side="left", expand=1, fill=tk.X, padx=10)
        tk.Button(frame_menu_bar, text="帮助?", command=help_info).pack(side="right", padx=5)
        tk.Button(frame_menu_bar, text="图片信息", command=show_pic_info).pack(side="right")
    
        # 显示照片-中间
        frame_content = tk.Frame(root, bg="white")
        tk.Button(frame_content, text="<<", command=btn_pre_click).grid(row=0, column=0, rowspan=2, sticky="ns", ipadx=15)
        tk.Button(frame_content, text=">>", command=btn_next_click).grid(row=0, column=2, rowspan=2, sticky="sn", ipadx=15)
    
        title, full_name = pic_list[0]
        # 图片标题
        print(title)
        # lbl_pic_title = tk.Label(frame_content, text=title)
        lbl_pic_title = tk.Label(frame_content)
        lbl_pic_title.grid(row=0, column=1)
        # 图片
        print(full_name)
        # img = Image.open(full_name)
        # photo = ImageTk.PhotoImage(img)
        # lbl_pic = tk.Label(frame_content, image=photo, bg="white")  # image属性绑定
        lbl_pic = tk.Label(frame_content, bg="white")  # image属性绑定
        lbl_pic.grid(row=1, column=1, sticky="ew")
        # 显示图片
        show_pic(title, full_name)
        # 表格指定列、行的拉伸
        frame_content.columnconfigure(1, weight=1)
        frame_content.rowconfigure(1, weight=1)
    
        # 按钮区-底部
        frame_btn = tk.Frame(root, bg="orange")
        tk.Button(frame_btn, text="第一张", command=btn_first_click).grid(row=0, column=0, padx=10, pady=10, ipadx=5, ipady=5)
        tk.Button(frame_btn, text="上一张", command=btn_pre_click).grid(row=0, column=1, padx=10, ipadx=5, ipady=5)
        btn_auto_play = tk.Button(frame_btn, text="自动播放", command=btn_auto_play_click)
        btn_auto_play.grid(row=0, column=2, padx=10, ipadx=5, ipady=5)
        tk.Button(frame_btn, text="下一张", command=btn_next_click).grid(row=0, column=3, padx=10, ipadx=5, ipady=5)
        tk.Button(frame_btn, text="最后一张", command=btn_last_click).grid(row=0, column=4, padx=10, ipadx=5, ipady=5)
    
        # 显示组件
        frame_menu_bar.pack(fill=tk.BOTH)
        frame_content.pack(expand=1, fill=tk.BOTH)
        frame_btn.pack()
    
        # 主循环
        root.mainloop()
    
    
    if __name__ == '__main__':
        main()
    

    图片查看器-草图

  • 相关阅读:
    如何修改SVN中的用户名和密码
    Telerik_2012_Q3 RadGrid 汉化
    DXperience-12.1.5 官网下载+注册破解+帮助文档
    Telerik_2012_Q3 (已破解)全套下载链接
    WinForm 控件库
    向项目的文件夹中写入数据流
    coolcarousel 图片轮播缩放问题
    IOS开发UI篇--使用CAShapeLayer实现复杂的View的遮罩效果
    iOS开发
    解析 iOS 动画原理与实现
  • 原文地址:https://www.cnblogs.com/yuexiao/p/12907475.html
Copyright © 2011-2022 走看看