您好,登錄后才能下訂單哦!
從本地文件夾中選取一張圖片并在canvas上顯示
from tkinter import * from tkinter import filedialog from PIL import Image, ImageTk if __name__ == "__main__": root = Tk() #setting up a tkinter canvas with scrollbars frame = Frame(root, bd=2, relief=SUNKEN) frame.grid_rowconfigure(0, weight=1) frame.grid_columnconfigure(0, weight=1) xscroll = Scrollbar(frame, orient=HORIZONTAL) xscroll.grid(row=1, column=0, sticky=E+W) yscroll = Scrollbar(frame) yscroll.grid(row=0, column=1, sticky=N+S) canvas = Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set) canvas.grid(row=0, column=0, sticky=N+S+E+W) xscroll.config(command=canvas.xview) yscroll.config(command=canvas.yview) frame.pack(fill=BOTH,expand=1) #function to be called when mouse is clicked def printcoords(): File = filedialog.askopenfilename(parent=root, initialdir="C:/",title='Choose an image.') filename = ImageTk.PhotoImage(Image.open(File)) canvas.image = filename # <--- keep reference of your image canvas.create_image(0,0,anchor='nw',image=filename) Button(root,text='choose',command=printcoords).pack() root.mainloop()
以上這篇Python button選取本地圖片并顯示的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。