生活随笔
收集整理的這篇文章主要介紹了
用python的tkinter库制作仿windows看图器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文原載于我的簡書,簡書界面干凈,更偏向于簡書一些,我的簡書
最近在學習python,就用python自己寫了一個仿windows的看圖器,在網上搜發現找不到相關的代碼,所以決定自己嘗試做了一個??磮D器要實現如下功能:
打開文件夾,找到相應文件 圖片可以進行等比例縮放 可以瀏覽同目錄問價下的上一張和下一張圖片
1.用label方法制作看圖器
由于python的tkinter庫只能打開gif文件不能打開jpg等其它文件,所以這里需要導入PIL庫。tkinter的學習建議參考莫煩的視頻。莫煩tkinter教程。講解非常詳細配有簡單案例適合初學者學習。
import tkinter
as tk
from PIL
import ImageTK
, Image
from tkinter
import filedialog root
= tk
. Tk
( )
root
. title
( '圖片查看器' )
root
. geometry
( '400x400' ) l
= tk
. Label
( root
, text
= 'pictures will show in this place' , image
= None )
l
. pack
( ) def openpicture ( ) : global imgfilename
= filedialog
. askopenfilename
( ) img
= ImageTk
. PhotoImage
( Image
. open ( filename
) ) l
. config
( image
= img
) b
= kt
. Button
( root
, text
= 'select a picture' , command
= openpicture
)
b
. pack
( ) tk
. mainloop
( )
我們開看一下運行效果: 點擊選擇按鈕: 選擇一張圖片: 這樣一個簡單的查看器就做完了,但是可以看到當圖片的像素太大的時候圖片無法顯示完全,所以需要對程序進行修改讓它能夠按標簽的大小進行縮放。
但是經過我多次測試,geometry大小設置的是400x400,label標簽大小設置的是300x300,但遺憾的是最后標簽填滿了整個窗口,猜測的原因可能是geometry和label的單位不同造成的,于是我改變了label的大小,設置為30x300,但最終標簽仍然充滿了整個窗口,查閱資料沒能解決是什么原因導致這一問題
2.用canvas方法制作看圖器
在label方法遇到困難后轉向了canvas方法,直接繪制畫布大小。由于每張圖片的尺寸不一樣,要想將圖片保持原來的長寬比顯示在canvas上需要將圖像進行縮放。 對函數進行縮放的方法參照這篇博文
import tkinter
as tk
from PIL
import ImageTK
, Image
from tkinter
import filedialog root
= tk
. Tk
( )
root
. title
( '圖片查看器' )
root
. geometry
( '500x500' ) canvas
= tk
. Canvas
( root
, height
= 400 , height
= 400 )
canvas
. pack
( ) def openpicture ( ) : global imgfilename
= filedialog
. askopenfilename
( ) image
= Image
. open ( filename
) w
, h
= image
. size mlength
= max ( w
, h
) mul
= 400 / mlength w1
= int ( w
* mul
) h1
= int ( h
* mul
) re_image
= image
. resize
( ( w1
, h1
) ) img
= ImageTK
. PhotoImage
( re_image
) canvas
. create_image
( 200 , 200 , anchor
= 'center' , image
= img
) b
= kt
. Button
( root
, text
= 'select a picture' , command
= openpicture
)
b
. pack
( ) tk
. mainloop
( )
瀏覽上一張和下一張
這里我的思想是:
先獲取該文件的完整路徑 獲取該文件的上一級路徑 獲取該文件的文件名 通過os.listdir()獲取該文件夾下的所有文件并生成列表 通過列表找到該文件的索引值 將索引值+1,-1實現上一張,下一張的功能 思想很簡單,在這里我將之前得分代碼重新整理,把不同功能進行了封裝
import tkinter
as tk
from PIL
import ImageTk
, Image
from tkinter
import filedialog
import osroot
= tk
. Tk
( )
root
. title
( '圖片查看器' )
root
. geometry
( '500x500' ) canvas
= tk
. Canvas
( root
, height
= 400 , width
= 400 )
canvas
. pack
( ) path
= tk
. StringVar
( ) def resize ( image
) : w
, h
= image
. sizemlength
= max ( w
, h
) mul
= 400 / mlength w1
= int ( w
* mul
) h1
= int ( h
* mul
) return image
. resize
( ( w1
, h1
) ) def show_image ( path
) : global img image
= Image
. open ( path
) re_image
= resize
( image
) img
= ImageTk
. PhotoImage
( re_image
) canvas
. create_image
( 200 , 200 , anchor
= 'center' , image
= img
) def openpicture ( ) :
global fileindex
, fatherpath
, files
, file_numfilepath
= filedialog
. askopenfilename
( ) fatherpath
= os
. path
. dirname
( filepath
) filename
= os
. path
. basename
( filepath
) files
= os
. listdir
( fatherpath
) file_num
= len ( files
) fileindex
= files
. index
( filename
) show_image
( filepath
) def previous ( ) : global fileindex
, fatherpath
, files
, file_numfileindex
-= 1 if fileindex
== - 1 : fileindex
= file_num
- 1 filepath1
= os
. path
. join
( fatherpath
, files
[ fileindex
] ) show_image
( filepath1
) def back ( ) : global fileindex
, fatherpath
, files
, file_numfileindex
+= 1 if fileindex
== file_num
: fileindex
= 0 filepath2
= os
. path
. join
( fatherpath
, files
[ fileindex
] ) show_image
( filepath2
) b
= tk
. Button
( root
, text
= 'select a picture' , command
= openpicture
)
b
. pack
( ) b1
= tk
. Button
( root
, text
= '上一張' , command
= previous
) . pack
( side
= 'left' )
b2
= tk
. Button
( root
, text
= '下一張' , command
= back
) . pack
( side
= 'right' ) tk
. mainloop
( )
總結
以上是生活随笔 為你收集整理的用python的tkinter库制作仿windows看图器 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。