如何在 Python tkinter 画布上绘制一张 png 图片?
要在 tkinter 中使用图片,Python 提供了 PIL 或 Pillow 工具包。它有许多内置函数可用于操作不同格式的图片。
要在画布窗口小部件中打开图片,我们需要使用 create_image(x, y, image, **options) 构造函数。当我们将图片值传递给该构造函数时,它将在画布中显示该图片。
示例
# Import the required libraries
from tkinter import *
from PIL import Image, ImageTk
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the window
win.geometry("700x600")
# Create a canvas widget
canvas=Canvas(win, width=700, height=600)
canvas.pack()
# Load the image
img=ImageTk.PhotoImage(file="Monalisa.png")
# Add the image in the canvas
canvas.create_image(350, 400, image=img, anchor="center")
win.mainloop()输出
运行以上代码会显示一个窗口,该窗口包含画布中的图片。

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP