如何在 Tkinter 画布项目中插入一张图片?
Tkinter 画布是 Tkinter 库中最通用的部件。它用于创建图像、形状、弧形、动画对象以及许多其他作品。为了处理和处理图像,Python 支持 Pillow 包或 PIL。我们可以使用 **create_image(width, height, image_location, options)** 方法在画布中添加图像作为项目。我们还可以通过定义锚 (options) 属性等位置参数,指定图像应在窗口中打开的位置。
示例
#Import the required Libraries
from tkinter import *
from PIL import Image,ImageTk
#Create an instance of tkinter frame
win = Tk()
#Set the geometry of tkinter frame
win.geometry("750x250")
#Create a canvas
canvas= Canvas(win, width= 600, height= 400)
canvas.pack()
#Load an image in the script
img= ImageTk.PhotoImage(Image.open("download.png"))
#Add image to the Canvas Items
canvas.create_image(10,10,anchor=NW,image=img)
win.mainloop()输出
运行以上代码,在画布中显示图像。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP