Python button选取本地图片并显示的实例
作者:YuMingJing_ 时间:2023-10-06 15:56:32
从本地文件夹中选取一张图片并在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()
来源:https://blog.csdn.net/YuMingJing_/article/details/79603579
标签:Python,button,图片,显示
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Python上基于Markov链生成伪随机文本的教程
2023-07-02 09:24:03
mysql 8.0.22压缩包完整安装与配置教程图解(亲测安装有效)
2024-01-25 05:26:14
![](https://img.aspxhome.com/file/2023/7/112897_0s.png)
读写xml文件的2个小函数
2007-08-23 12:59:00
如何将 awk 脚本移植到 Python
2022-02-28 05:40:52
python获取天气接口给指定微信好友发天气预报
2023-08-11 18:13:06
![](https://img.aspxhome.com/file/2023/5/125165_0s.png)
python使用pil生成图片验证码的方法
2022-03-08 18:39:24
JSQL 批量图片切换的实现代码
2023-09-05 06:47:59
ASP编程常用的分页代码
2007-09-11 13:57:00
SqlServer如何通过SQL语句获取处理器(CPU)、内存(Memory)、磁盘(Disk)以及操作系统相关信息
2024-01-17 20:18:16
![](https://img.aspxhome.com/file/2023/8/94448_0s.png)
Vue配置文件vue.config.js配置前端代理方式
2023-07-02 16:39:06
解析python 类方法、对象方法、静态方法
2022-10-08 04:47:57
Go语言标准输入输出库的基本使用教程
2023-08-25 19:44:11
![](https://img.aspxhome.com/file/2023/3/101053_0s.png)
解决bootstrap导航栏navbar在IE8上存在缺陷的方法
2023-09-03 10:55:07
![](https://img.aspxhome.com/file/2023/0/56080_0s.gif)
mysql数据库sql优化原则(经验总结)
2024-01-22 17:55:12
Python初学者必备的文件读写指南
2023-03-16 11:44:15
不是原型继承那么简单!prototype的深度探索
2008-03-07 12:42:00
Mysql的服务无法启动的1067错误解决
2012-01-05 19:31:56
python中altair可视化库实例用法
2021-04-21 17:32:54
![](https://img.aspxhome.com/file/2023/3/89013_0s.jpg)
Oracle数据库的备份与恢复
2010-07-28 12:52:00
python循环控制之break和continue流程控制语句
2021-02-05 15:22:29