使用python的tkinter模块实现图形化界面查看图片
#点击上一张、下一张显示图片
from tkinter import *
from PIL import Image,ImageTk
count=0
path = 'resource/'
def up():
global count
count+=1
if count>4:
count=1
img = Image.open(path + str(count)+'.jpg')
img = ImageTk.PhotoImage(img)
label.config(image=img)
label.image = img
def down():
global count
count-=1
if count<1:
count=4
img = Image.open(path + str(count)+'.jpeg')
img = ImageTk.PhotoImage(img)
label.config(image=img)
label.image = img
root=Tk()
root.title('相册')
path='resource/'
img=Image.open(path+'1.jpeg')
img=ImageTk.PhotoImage(img)
box=Frame(root, width=600,height=400,borderwidth=5)
box.grid(row=0, column=0)
label=Label(box,width=600,height=337,image=img)
label.grid(row=0,columnspan=2)
btnup=Button(box,text="上一张", command=up)
btndown=Button(box,text="下一张", command=down)
btnup.grid(row=1, column=0)
btndown.grid(row=1, column=1)
root.mainloop()
实现后的界面如下:
点击上一站、下一张后会自动切换图片。图片文件如图:
tkinter这个库做界面还是不错的,使用起来也简单方便,对于像我这样的新手来说很友好。特别是以后在使用python写一些图形化的工具是很有用。