网站地图    收藏   

主页 > 后端 > python >

Python button选取本地图片并显示的实例

来源:自学PHP网    时间:2019-08-07 16:35 作者:小飞侠 阅读:

[导读] Python button选取本地图片并显示的实例...

从本地文件夹中选取一张图片并在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选取本地图片并显示的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自学php网。

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论