使用python中tkinter库显示图片点击按钮跳跳下一张图片
生活随笔
收集整理的這篇文章主要介紹了
使用python中tkinter库显示图片点击按钮跳跳下一张图片
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
實現(xiàn)的主要功能:
? ? ? ? 1、 顯示圖片
? ? ? ? 2、 按鈕按下,或者上下左右鍵切換 顯示上一張圖片,下一張圖片
? ? ? ? 3、 跳轉按鈕可以跳轉到指定圖片位置
? ? ? ? 4、 對圖片評價并保存到文本中
使用方法:
????????在代碼同級目錄下新建一個png文件夾,將想要瀏覽的圖片放入即可
????????
????????軟件界面:
?????????
?代碼結構簡單就很少注釋了,僅供個人娛樂學習。
? ? ? ? 直接給代碼:
import datetime import tkinter as tk from PIL import Image, ImageTk import osFilePath = 'png' PngList = os.listdir(FilePath)def Writediscuss(strdiscuss):f = open("discuss.txt",'a',encoding='utf8')strTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S ')f.write(strTime + strdiscuss)f.close()def resize(nX,nY):xNew = 640yNew = int(nY*640/nX)return (xNew,yNew)root = tk.Tk() root.geometry("1600x900") root.title('')img = Image.open('png\\T1 (1).jpeg') # 打開圖片 photo = ImageTk.PhotoImage(img) # imglabel = tk.Label(root, image=photo) imglabel.place(x=0,y=0)nIndex = 0 # 記錄顯示圖片的文件下標def submit():global nIndexenTextJumNum.delete(0.0, tk.END)enTextJumNum.insert(tk.INSERT, str(nIndex))enTextDiscuss.delete(0.0, tk.END)strDiscuss = PngList[nIndex].split('.')[0] +": "enTextDiscuss.insert(tk.INSERT, strDiscuss)print(PngList[nIndex])strPng = FilePath + '\\'+PngList[nIndex]global photoglobal imglabelphoto = ImageTk.PhotoImage(file=strPng) # 打開圖片看分辨率,如果太大則需要調整photoNew = Image.open(strPng).resize(resize(photo.width(),photo.height())) # 00調整圖片的分辨率photo = ImageTk.PhotoImage(photoNew)imglabel.config(image=photo)nIndex = nIndex+1def jumpTo():global nIndexnIndex = int(enTextJumNum.get('0.0', 'end'))print(nIndex)submit()# 上一個 def pre():global nIndexnIndex = nIndex - 2print(nIndex)submit()# 上一個 def home():global nIndexnIndex = 0print(nIndex)submit()# 寫入評論 def writeDiscuss():strDiscuss = enTextDiscuss.get('0.0', 'end')strDiscuss = PngList[nIndex] +": " + strDiscussWritediscuss(strDiscuss)def onEventNext(e):submit()def onEventPre(e):pre()# 輸入控件 enTextJumNum = tk.Text(root) enTextJumNum.place(x=400,y=800,height=20, width=58) enTextJumNum.insert(tk.INSERT,'0')# 跳轉控件 btnjump = tk.Button(root, text="跳轉", command=jumpTo) btnjump.place(x=500,y=800)# 下一張 btnNext = tk.Button(root, text="下一個", command=submit) btnNext.place(x=280,y=800)# 上一張 btnPre = tk.Button(root, text="上一個", command=pre) btnPre.place(x=200,y=800)# 首頁 btnPre = tk.Button(root, text="首頁", command=home) btnPre.place(x=120,y=800)# 輸入評論控件 enTextDiscuss = tk.Text(root) enTextDiscuss.place(x=800,y=600,height=200, width=300) enTextDiscuss.insert(tk.INSERT,'評論')# 評論寫入文件 btnDiscuss = tk.Button(root, text="批語", command=writeDiscuss) btnDiscuss.place(x=800,y=800)root.bind("<MouseWheel>",onEventNext) root.bind("<Down>",onEventNext) root.bind("<Right>",onEventNext)root.bind("<Up>",onEventPre) root.bind("<Left>",onEventPre) tk.mainloop()總結
以上是生活随笔為你收集整理的使用python中tkinter库显示图片点击按钮跳跳下一张图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信银行突破单一模式功能日渐强大
- 下一篇: Unity中UI组件