日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

用python的tkinter库制作仿windows看图器

發布時間:2023/12/20 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用python的tkinter库制作仿windows看图器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文原載于我的簡書,簡書界面干凈,更偏向于簡書一些,我的簡書

最近在學習python,就用python自己寫了一個仿windows的看圖器,在網上搜發現找不到相關的代碼,所以決定自己嘗試做了一個。看圖器要實現如下功能:

  • 打開文件夾,找到相應文件
  • 圖片可以進行等比例縮放
  • 可以瀏覽同目錄問價下的上一張和下一張圖片

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)) #tkinter只能打開gif文件,這里用PIL庫# 打開jpg格式的文件l.config(image=img) #用config方法將圖片放置在標簽中b=kt.Button(root,text='select a picture', command=openpicture) #設置按鈕,并給它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) #畫布長款定為400x400 canvas.pack()def openpicture():global imgfilename=filedialog.askopenfilename() #獲取文件全路徑image=Image.open(filename) #打開圖片放到image中w,h=image.size #獲取image的長和寬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中展示圖片canvas.create_image(200,200,anchor='center',image=img) #以中小點為錨點b=kt.Button(root,text='select a picture', command=openpicture) #設置按鈕,并給它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 #要申明全局變量我猜測是調用了canvasimage = Image.open(path) # 打開圖片re_image = resize(image) # 調用函數img = ImageTk.PhotoImage(re_image) # PhotoImage類是用來在label和canvas展示圖片用的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 -=1if fileindex == -1:fileindex = file_num-1filepath1=os.path.join(fatherpath, files[fileindex])show_image(filepath1)def back():global fileindex, fatherpath, files,file_numfileindex += 1if fileindex == file_num:fileindex = 0filepath2 = 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看图器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。