Python button選取本地圖片并顯示的實(shí)例
從本地文件夾中選取一張圖片并在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選取本地圖片并顯示的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Python爬取fofa網(wǎng)頁(yè)端數(shù)據(jù)過(guò)程解析
這篇文章主要介紹了基于Python爬取fofa網(wǎng)頁(yè)端數(shù)據(jù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
python ndarray數(shù)組對(duì)象特點(diǎn)及實(shí)例分享
在本篇文章里小編給大家分享的是一篇關(guān)于python ndarray數(shù)組對(duì)象特點(diǎn)及實(shí)例相關(guān)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。2021-10-10
python tkinter Entry控件的焦點(diǎn)移動(dòng)操作
這篇文章主要介紹了python tkinter Entry控件的焦點(diǎn)移動(dòng)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
在Python3中使用asyncio庫(kù)進(jìn)行快速數(shù)據(jù)抓取的教程
這篇文章主要介紹了在Python3中使用asyncio進(jìn)行快速數(shù)據(jù)抓取,asyncio是一個(gè)異步IO庫(kù),運(yùn)行效率較高,需要的朋友可以參考下2015-04-04
Python數(shù)據(jù)類型--字典dictionary
這篇文章主要介紹了Python數(shù)據(jù)類型字典dictionary,字典是另一種可變?nèi)萜髂P?,且可存?chǔ)任意類型對(duì)象。下面詳細(xì)內(nèi)容需要的小伙伴可以參考一下,希望對(duì)你有所幫助2022-02-02
CentOS 7下安裝Python 3.5并與Python2.7兼容并存詳解
這篇文章主要給大家介紹了在CentOS 7下安裝Python 3.5并與Python2.7兼容并存的相關(guān)資料,文中將安裝步驟介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07

