当前位置:网站首页>Detour of Tkinter picture scaling

Detour of Tkinter picture scaling

2022-07-05 07:28:00 work-harder

background :

        win8.1, python 3.9.7, Bring their own IDLE

Purpose :

         stay canvas Show the complete picture in

Method :

         On the whole , periphrasis , Not found yet tkinter Of PhotoImage How to zoom .

        use PIL Of Image and ImageTk After detour , Submit it to canvas.create_image.

from tkinter import *
from PIL import Image #  Installation method : pip install pillow,  Name and PIL Different .
from PIL import ImageTk


class Application(Frame):

    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.createWidget()

    def createWidget(self):
        self.canvas = Canvas(self, width=300,height=200, bg="green")
        self.canvas.pack()
        line = self.canvas.create_line(10,10,30,20,40,50)
        rect = self.canvas.create_rectangle(50,50,100,100)
        oval = self.canvas.create_oval(50,50,100,100)

        global photo
        #photo = PhotoImage(file="imgs/flowers.gif") # tkinter Of , Can't zoom 
        img1 = Image.open("imgs/flowers.gif")
        img1 = img1.resize((200,100)) # The original image 3840x2160
        photo = ImageTk.PhotoImage(image=img1,size=50) #PIL Of 
        self.canvas.create_image(200,150,image=photo)
        #print("size of photo:{0},{1}".format(photo.width(), photo.height()))
        # hold photo Switch to  img1, You can see that img1 Picture size for 



if __name__ == "__main__":
    root = Tk()
    root.geometry("400x300+200+200")
    app = Application(master=root)
    root.mainloop()

原网站

版权声明
本文为[work-harder]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140555475497.html