如何在python中使用画布框架调整图像大小

时间:2019-12-12 10:14:07

标签: python canvas tkinter

在这里,我尝试使用画布框显示图像,但未显示任何内容。 这有什么问题。 当我在Label小部件中用root替换框架时,它正在工作。 谢谢

from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
import tkinter as tk
root = Tk()
root.title("Title")
root.geometry('600x600')

def resize_image(event):
    new_width = event.width
    new_height = event.height
    image = copy_of_image.resize((new_width, new_height))
    photo = ImageTk.PhotoImage(image)
    label.config(image = photo)    
    label.image = photo 
def on_configure(event):

    canvas.configure(scrollregion=canvas.bbox('all'))

scrollbary = Scrollbar(root, orient = tk.VERTICAL)
scrollbarx = Scrollbar(root,orient = tk.HORIZONTAL)

canvas = tk.Canvas(root,yscrollcommand = scrollbary.set,
                       xscrollcommand = scrollbarx.set)
scrollbary.config(command=canvas.yview)
scrollbarx.config(command=canvas.xview)

scrollbary.pack(side=tk.RIGHT, fill='y',expand = tk.FALSE)

scrollbarx.pack(side=tk.BOTTOM,fill = 'x',expand = tk.FALSE)

canvas.pack(side=tk.LEFT, padx=5, pady=5,
                                                 fill=tk.BOTH, expand=tk.TRUE)

canvas.bind('<Configure>', on_configure)
frame = tk.Frame(canvas)
canvas.create_window((0,0), window=frame, anchor='nw')

image = Image.open('logo1.png')
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = ttk.Label(frame, image = photo)
label.bind('<Configure>', resize_image)
label.pack(fill=BOTH, expand = YES)
root.mainloop()

0 个答案:

没有答案