python tkinter库实例
from PIL import Image, ImageTk # 导入图像处理函数库
import tkinter as tk # 导入GUI界面函数库
import os
import os.path
import json
import time
# 创建窗口 设定大小并命名
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))
# 创建打开图像和显示图像函数
def show_img(n=0):
try:
global label_Img,filepath,imglist,length,num
global Img_re # 必须设置为全局变量,不然变量被回收无法显示图片
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)
# 创建显示图像按钮
label1 = tk.Label(fm3,text='filepath:',width=15,height=2)
btn_Show3 = tk.Button(fm3,
text='打开', # 显示在按钮上的文字
width=15, height=2,
command=show_img) # 点击按钮式执行的命令
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) # 点击按钮式执行的命令
btn_Show.pack(side=tk.LEFT) # 按钮位置
btn_Show2 = tk.Button(fm2,
text='下一张', # 显示在按钮上的文字
width=15, height=2,
command=follow) # 点击按钮式执行的命令
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) # 点击按钮式执行的命令
btn_Show3.pack(side=tk.LEFT) # 按钮位置
# frame 自动左右居中
##
# 运行整体窗口
def changeImage():
if cir:
follow()
window.after(1000,changeImage)
window.after(1000,changeImage)
window.mainloop()