即使参考已保存,Tkinter画布图像也不显示

时间:2017-09-01 19:19:59

标签: python python-2.7 tkinter pillow tkinter-canvas

所以我有这段代码:

def addTux(self, h,w,g):
    global root
    cx = h/(2*g)
    cy = w/(2*g)

    img = Image.open("sprites/tux.png")

    img.thumbnail((g,g))
    print "The size of the Image is: "
    print(img.format, img.size, img.mode)
    # img.show()
    self.p_sprite = ImageTk.PhotoImage(img)
    root.p_sprite = self.p_sprite
    self.tux = self.canvas.create_image(cx*g, cy*g, image=self.p_sprite)

    c = self.canvas.coords(self.tux)
    print c

print c打印coords ... img.show()打开我的penguiny缩略图(当它没有被注释掉时)...... 但是Tkinter没有表现出下蹲。

稍后会调用

self.canvas.pack(),我将保存两个对PhotoImage的引用,以确保它不会被垃圾回收。

我得到的只是一个白色的屏幕。

我确定它是湿软件中的短片......只是不确定在哪里。有什么想法吗?

以下所有代码:

app.py:

from Tkinter import *
from PIL import Image, ImageTk
import constants

class Application(Frame):
    def addTux(self, h,w,g):
        global root
        cx = h/(2*g)
        cy = w/(2*g)

        img = Image.open("sprites/tux.png")
        root.img = img

        img.thumbnail((g,g))
        print "The size of the Image is: "
        print(img.format, img.size, img.mode)
        img.show()
        self.p_sprite = ImageTk.PhotoImage(img)
        root.p_sprite = self.p_sprite
        self.tux = self.canvas.create_image(cx*g, cy*g, image=self.p_sprite)
        self.canvas.pack()

        c = self.canvas.coords(self.tux)
        print c

    def createTiles(self):
        h = constants.bounds["y"][1]
        w = constants.bounds["x"][1]
        g = constants.grid_size

        self.grid = []
        # for i in range(0,(h/g)):
        #     # self.grid.append([])
        #     # print self.grid[i]
        #     for j in range(0,(w/g)):
        #         s = self.canvas.create_rectangle(j * g, i*g, (j+1)*g, (i+1)*g, fill="green")
        #         # self.grid[i].append(s)
        self.addTux(h,w,g)
        self.canvas.pack()

    def createWidgets(self):
        global root
        self.frame = Frame(root)
        self.frame.pack(fill=BOTH, expand=1)
        self.canvas=Canvas(self.frame, height=constants.bounds["y"][1]+10, width=constants.bounds["x"][1]+10, background="red")
        self.canvas.pack(fill=BOTH, expand=1)

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()
        self.createTiles()
        print self.p_sprite

root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

constants.py:

grid_size=40

bounds = {
    "x": [0,1000],
    "y": [0,720]
}

0 个答案:

没有答案