python tkinter庫實例
from PIL import Image, ImageTk # 導(dǎo)入圖像處理函數(shù)庫
import tkinter as tk? ? ? ? ? ?# 導(dǎo)入GUI界面函數(shù)庫
import os
import os.path
import json
import time
# 創(chuàng)建窗口 設(shè)定大小并命名
window = tk.Tk()
window.title('模擬圖像查看器')
window.geometry('700x700')
##window.resizable(0,0)
paned=tk.PanedWindow(window)
#paned.pack(side=tk.BOTTOM)
# initiate
filepath=r'C:\Users\Public\Pictures\Sample Pictures\\'
imglist=[imgname for imgname in os.listdir(filepath) if imgname.endswith(('.jpeg','.jpg','.png','.bmp','.gif'))]
length = len(imglist)
global num
num=0
fm3=tk.Frame(window,height=20)
fm3.pack(padx=10,pady=10,side=tk.TOP,ipadx=0)
fm2=tk.Frame(window,height=20)
fm2.pack(padx=10,pady=10,side=tk.TOP)
fm4=tk.Frame(window,height=20)
fm4.pack(padx=10,pady=10,side=tk.TOP)
fm1=tk.Frame(window)
fm1.pack(padx=10,pady=10,side=tk.TOP)
t1=tk.StringVar()
t1.set(filepath)
errortext=tk.StringVar()
errortext.set('? ? ? ')
labelerror = tk.Label(fm3,textvariable=errortext,\
width=15,height=2,fg='red',font=("微軟雅黑", 20))
# 創(chuàng)建打開圖像和顯示圖像函數(shù)
def show_img(n=0):
? ? try:
? ? ? ? global label_Img,filepath,imglist,length,num
? ? ? ? global Img_re # 必須設(shè)置為全局變量,不然變量被回收無法顯示圖片
? ? ? ? num=n
? ? ? ? if filepath !=t1.get():
? ? ? ? ? ? filepath = t1.get()
? ? ? ? ? ? imglist=[imgname for imgname in os.listdir(filepath) if imgname.endswith(('.jpeg','.jpg','.png','.bmp','.gif'))]
? ? ? ? ? ? length = len(imglist)
? ? ? ? img = Image.open(os.path.join(filepath,imglist[n]))
? ? ? ? [w,h]=img.size
? ? ? ? shift = max(w/500,h/500,1)
? ? ? ? Img_re = img.resize((round(w/shift),round(h/shift)),Image.ANTIALIAS)
##? ? ? ? paned.photo = ImageTk.PhotoImage(Img_re)
? ? ? ? Img_re = ImageTk.PhotoImage(Img_re)
? ? ? ? filetext.set(imglist[n])
? ? ? ? if 'label_Img' not in globals():
##? ? ? ? ? ? label_Img = tk.Label(fm1,image=paned.photo)
? ? ? ? ? ? label_Img = tk.Label(fm1,image=Img_re)
? ? ? ? ? ? label_Img.pack(side=tk.TOP)
? ? ? ? else:
##? ? ? ? ? ? label_Img.configure(image=paned.photo)
? ? ? ? ? ? label_Img.configure(image=Img_re)
? ? ? ? errortext.set('? ? ? ')
? ? except:
? ? ? ? errortext.set('error!')
def follow():
? ? global num
? ? num = (num+1)%length
? ? show_img(num)
def previous():
? ? global num? ?
? ? num = (num-1)%length
? ? show_img(num)
# 創(chuàng)建顯示圖像按鈕
label1 = tk.Label(fm3,text='filepath:',width=15,height=2)
btn_Show3 = tk.Button(fm3,
? ? text='打開',? ? ? # 顯示在按鈕上的文字
? ? width=15, height=2,
? ? command=show_img)? ? ?# 點擊按鈕式執(zhí)行的命令
btn_Show3.pack(side=tk.LEFT)? ? # 按鈕位置
label1.pack(side=tk.LEFT,pady=10,padx=0,ipadx=0)
entry=tk.Entry(fm3,textvariable=t1).\
pack(side=tk.LEFT,pady=10,padx=0,ipadx=0)
labelerror.pack(side=tk.LEFT,pady=10,padx=0,ipadx=0)
filetext=tk.StringVar()
filetext.set('')
btn_file = tk.Label(fm4,textvariable=filetext,\
height=2,fg='red',font=("微軟雅黑", 20))
btn_file.pack(side=tk.BOTTOM,pady=10,padx=0,ipadx=0)
show_img(num)
btn_Show = tk.Button(fm2,
? ? text='上一張',? ? ? # 顯示在按鈕上的文字
? ? width=15, height=2,
? ? command=previous)? ? ?# 點擊按鈕式執(zhí)行的命令
btn_Show.pack(side=tk.LEFT)? ? # 按鈕位置
btn_Show2 = tk.Button(fm2,
? ? text='下一張',? ? ? # 顯示在按鈕上的文字
? ? width=15, height=2,
? ? command=follow)? ? ?# 點擊按鈕式執(zhí)行的命令
btn_Show2.pack(side=tk.LEFT)? ? # 按鈕位置
cir =0
def changecir():
? ? global cir
? ? cir = 1-cir
? ? if cir:
? ? ? ? textcon.set('停止播放')
? ? ? ? btn_Show3['bg']='yellow'
? ? ? ? btn_Show2['state']='disable'
? ? ? ? btn_Show['state']='disable'
? ? else:
? ? ? ? textcon.set('自動播放')
? ? ? ? btn_Show3['bg']='SystemButtonFace'
? ? ? ? btn_Show2['state']='normal'
? ? ? ? btn_Show['state']='normal'
textcon=tk.StringVar()
textcon.set('自動播放')
btn_Show3 = tk.Button(fm2,
? ? textvariable=textcon,? ? ? # 顯示在按鈕上的文字
? ? width=15, height=2,
? ? disabledforeground ='grey',
? ? command= changecir)? ? ?# 點擊按鈕式執(zhí)行的命令
btn_Show3.pack(side=tk.LEFT)? ? # 按鈕位置
# frame 自動左右居中
##
# 運行整體窗口
def changeImage():
? ? if cir:
? ? ? ? follow()
? ? window.after(1000,changeImage)
window.after(1000,changeImage)
window.mainloop()