tkinter放大缩小图片
时间: 2023-07-13 19:12:19
浏览: 168
要在Tkinter中放大缩小[图片](https://geek.csdn.net/educolumn/1de9845baad351038ab25b200eff4431?spm=1055.2569.3001.10083),可以使用Pillow(Python Ima[gin](https://geek.csdn.net/educolumn/1aef582107c22aa4d9f243890549bf39?spm=1055.2569.3001.10083)g Library)库来处理[图片](https://geek.csdn.net/educolumn/1de9845baad351038ab25b200eff4431?spm=1055.2569.3001.10083)。以下是一个简单的例子,展示了如何在Tkinter中放大缩小[图片](https://geek.csdn.net/educolumn/1de9845baad351038ab25b200eff4431?spm=1055.2569.3001.10083):
```python
from PIL import Image, ImageTk
import tkinter as tk
class App:
def __init__(self, root):
self.root = root
self.canvas = tk.Canvas(root)
self.canvas.pack()
# 打开图片
self.image = Image.open("example.jpg")
self.photo = ImageTk.PhotoImage(self.image)
# [显示](https://geek.csdn.net/educolumn/103ca72607c717122e068b9f06a24df6?spm=1055.2569.3001.10083)图片
self.image_item = self.canvas.create_image(0, 0, image=self.photo, anchor="nw")
# 添加放大缩小按钮
self.zoom_in_button = tk.Button(root, text="放大", command=self.zoom_in)
self.zoom_in_button.pack(side="left")
self.zoom_out_button = tk.Button(root, tex
```