Python-A photoviewer

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Author: Liye Z
#Date:2019.08.18   14:30
import tkinter as tk
from tkinter.filedialog import askopenfilename
from PIL import Image,ImageTk


root=tk.Tk()
#Step one: initiate the root gui window.
root.title("Photo viewer 1.0(JPEG)")
root.geometry("800x600")
#Step two: add a text label.
title_lable=tk.Label(root,text='Welcome to Photoviewer 1.0',bg='yellow',font=('Arial',12),width=24, height=1,anchor=tk.CENTER)
title_lable.grid(row=0,column=1)
#Step three: add a Canvas.
pic_window=tk.Canvas(root,bg='#E4E4E4',height=500, width=720)
pic_window.grid(row=1,column=1)
#Step four: Add Button Command
def open_command():
    file_path=askopenfilename(title='Select a picture',filetypes=[('JPG','*.jpg')],initialdir='C:\\Windows')
    print(file_path)
    global image,im
    image=Image.open(file_path)
    w,h=image.size
    print(w)
    print(h)
    image_resized=resize(500,720,image)
    im = ImageTk.PhotoImage(image_resized)
    pic_window.create_image(350, 280, anchor='center', image=im)
    # ... add a button.
#resize function:https://blog.csdn.net/sinat_27382047/article/details/80138733
def resize(w_box, h_box, pil_image) :  # 参数是:要适应的窗口宽、高、Image.open后的图片
    w, h = pil_image.size  # 获取图像的原始大小
    f1 = 1.0 * w_box / w
    f2 = 1.0 * h_box / h
    factor = min([f1, f2])
    width = int(w * factor)
    height = int(h * factor)
    return pil_image.resize((width, height), Image.ANTIALIAS)
open_button=tk.Button(root,text='Open',bg='light gray',font=('Arial',11),width=4,height=1,anchor=tk.CENTER,relief='groove',command=open_command)
open_button.grid(row=0,column=0)
#Step Five:
bottom_lable=tk.Label(root,text='Only Support JPEG Format--By Author',bg='white',font=('Arial',12),width=30, height=1,anchor=tk.CENTER)
bottom_lable.grid(row=2,column=1)
root.mainloop()

猜你喜欢

转载自blog.csdn.net/you_us/article/details/99708163