为什么我需要Class Member在Canvas上显示图像?

时间:2017-07-01 18:43:43

标签: python-3.x tkinter raspberry-pi python-3.5 tkinter-canvas

import tkinter as tk

from PIL import Image, ImageTk

class ImageViewer(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.screen_width = self.winfo_screenwidth()
        self.screen_height = self.winfo_screenheight()

        self.geometry("%dx%d%+d%+d" % (self.screen_width, 
                        self.screen_height, 0, 0))
  

将图像放在白色背景上

       self.canvas = tk.Canvas(self, bg='white')

       self.canvas.config(height=self.screen_height, 
                 width=self.screen_height, highlightthickness=0)
       self.canvas.pack()
  

如果我尝试使用变量image_tk代替self.Image_tk,则不会显示图片

    def show_image(self):
        image = Image.open("./image1.jpg")

        image_width, image_height = image.size

        window_width = int(self.canvas['width'])
        window_height = int(self.canvas['height'])
        window_centre_x = window_width / 2
        window_centre_y = window_height / 2

        if image_width > window_width or image_height > window_height:

            image.thumbnail((window_width, window_height), 
               Image.ANTIALIAS)
            self.image_tk = ImageTk.PhotoImage(image)
            self.canvas.create_image(window_centre_x, window_centre_y, 
                image=self.image_tk, anchor=tk.CENTER, tag='i')
        else:
            scale_x = float(window_width) / image_width
            scale_y = float(window_height) / image_height

            if scale_x > scale_y:
               scale = scale_y
            else:
               scale = scale_x

            scaled_width = int(image_width * scale)
            scaled_height = int(image_height * scale)

            image = image.resize((scaled_width, scaled_height), 
                           Image.ANTIALIAS)
            self.image_tk = ImageTk.PhotoImage(image)
            self.canvas.create_image(window_centre_x, window_centre_y, 
                 image=self.image_tk, anchor=tk.CENTER, tag='i')

img = ImageViewer()
img.show_image()
img.mainloop()

1 个答案:

答案 0 :(得分:1)

通常,当您只使用WITH k AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY ShiftId ORDER BY ValidFrom) AS _RN FROM KrisisShifts_ShiftTrade /*FOR SYSTEM_TIME ALL*/ ), T AS (SELECT k.*, _colname = n.n.value('local-name(.)[1]', 'sysname'), _colvalue = n.n.value('text()[1]', 'nvarchar(4000)') FROM k CROSS apply (SELECT (SELECT k.* FOR xml path('row'), elements xsinil, type)) ca(x) CROSS APPLY x.nodes('/row/*[not(self::_RN or self::ValidFrom or self::ValidTo)]') n(n)) SELECT T.ShiftId, T._colname AS [Column], T._colvalue AS value, t.ValidFrom, T.ValidTo FROM T T INNER JOIN T Tnext ON Tnext._RN = T._RN + 1 AND T.ShiftId = Tnext.ShiftId AND T._colname = Tnext._colname WHERE EXISTS(SELECT T._colvalue EXCEPT SELECT Tnext._colvalue) ORDER BY ShiftId, [Column], ValidFrom; 时,Python会在垃圾收集中捕获它并将其删除。为了防止这种情况发生,即实际显示图像,你必须将它绑定到更永久的东西,而不是在垃圾收集中捕获。其中一种方法是在一个类中,通过引用image_tk

因此,实际上并不是能够让图像显示出来的类,而是附着在'附近的事实。到你班级的self,它不会被Python清除,因此让它出现。