詳解python tkinter包獲取本地絕對(duì)路徑(以獲取圖片并展示)
實(shí)例代碼:
import tkinter as tk import tkinter.filedialogimport cv2 def choose_file(): # 選擇文件 selectFileName = tk.filedialog.askopenfilename(title=’選擇文件’) e.set(selectFileName) def show(e_entry): #顯示圖片 img = cv2.imread(e_entry.get()) cv2.imshow('PICTURE',img) cv2.waitKey(0) def window(): root = tk.Tk() root.geometry(’650x450+150+100’) root.title(’test’) root.resizable(False, False) global e e = tk.StringVar() #文本輸入框 e_entry = tk.Entry(root, width=68,textvariable=e) #e_entry.pack() #選擇文件控件 sumbit_btn = tk.Button(root,text='選擇文件',bg=’yellow’,command = choose_file) sumbit_btn.pack() #展示文件控件 show_btn = tk.Button(root,text= ’查看圖片’,bg = ’blue’, command = lambda :show(e_entry)) show_btn.pack() root.mainloop() window()
效果如圖:
選擇文件:
查看圖片效果:(左邊為本地,右邊為查看)
說(shuō)明:查看圖片我用了cv2中的imshow函數(shù),也有一些其他的方法,嘗試沒(méi)嘗試出來(lái),所以就用了這種簡(jiǎn)單取巧方法。
總結(jié)
到此這篇關(guān)于python tkinter包獲取本地絕對(duì)路徑(以獲取圖片并展示)的文章就介紹到這了,更多相關(guān)python tkinter包獲取本地絕對(duì)路徑內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
