#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2022/04/08 05:05:46 +0900> """ Canvas scale sample. * Windows10 x64 21H2 + Python 3.9.12 64bit + Pillow 9.1.0 * Windows10 x64 21H2 + Python 2.7.18 32bit + Pillow 6.2.2 """ try: # Python 3.x import tkinter as tk except Exception: # Python 2.7 import Tkinter as tk from PIL import Image, ImageTk class App(tk.Frame, object): """My App.""" def __init__(self, master=None): tk.Frame.__init__(self, master) self.master.title("Canvas sample") self.master.geometry("600x600") self.frm = tk.Frame(self.master) self.frm.pack(expand=1, fill=tk.BOTH) self.frm.rowconfigure(1, weight=1) self.frm.columnconfigure(0, weight=1) # create Button, Label self.bfrm = tk.Frame(self.frm) self.bfrm.grid(row=0, column=0, sticky=tk.W + tk.E) self.btn0 = tk.Button(self.bfrm, text="-", command=lambda: self.dec_ratio()) self.btn1 = tk.Button(self.bfrm, text="+", command=lambda: self.inc_ratio()) self.lblstr = tk.StringVar() self.lblstr.set("100%") self.lbl = tk.Label(self.bfrm, textvariable=self.lblstr) self.btn0.pack(side=tk.LEFT, ipadx=8, padx=4) self.btn1.pack(side=tk.LEFT, ipadx=8, padx=4) self.lbl.pack(side=tk.LEFT, ipadx=8, padx=8) # create Canvas self.canvas = tk.Canvas(self.frm, bg="#666666") self.canvas.grid(row=1, column=0, sticky=tk.N + tk.S + tk.W + tk.E) # load image self.im = Image.open("lena_std.png") self.photo_image = ImageTk.PhotoImage(image=self.im) self.now_ratio = 1.0 self.tgt_ratio = 1.0 self.update() cw = self.canvas.winfo_width() ch = self.canvas.winfo_height() # draw image self.canvas.create_image(cw / 2, ch / 2, image=self.photo_image) # darw Rectangle, Oval, Polygon x = cw / 2 y = ch / 2 self.canvas.create_rectangle(x, y, x + 24, y + 96, width=4, outline="#ff0000") self.canvas.create_oval(x + 32, y + 32, x - 32, y - 32, width=6, outline="#00ff00") self.canvas.create_polygon(x, y, x - 40, y - 60, x - 80, y + 30, width=2, outline="#0000ff", fill="") def inc_ratio(self): self.tgt_ratio = self.tgt_ratio + 0.5 self.tgt_ratio = min([12.0, self.tgt_ratio]) self.lblstr.set("%d%%" % int(self.tgt_ratio * 100)) self.scale_canvas() def dec_ratio(self): self.tgt_ratio = self.tgt_ratio - 0.5 self.tgt_ratio = max([0.5, self.tgt_ratio]) self.lblstr.set("%d%%" % int(self.tgt_ratio * 100)) self.scale_canvas() def scale_canvas(self): v = self.tgt_ratio / self.now_ratio cw = self.canvas.winfo_width() ch = self.canvas.winfo_height() self.canvas.scale(tk.ALL, cw / 2, ch / 2, v, v) self.now_ratio = self.now_ratio * v def main(): root = tk.Tk() app = App(master=root) app.mainloop() if __name__ == '__main__': main()