将数据设置为tkinter背景?

时间:2011-09-19 22:46:47

标签: python tkinter

所以我找到了这段代码:

import Image,ImageDraw
import cStringIO
from random import randint as rint

def randgradient():
    img = Image.new("RGB", (300,300), "#FFFFFF")
    draw = ImageDraw.Draw(img)

    r,g,b = rint(0,255), rint(0,255), rint(0,255)
    dr = (rint(0,255) - r)/300.
    dg = (rint(0,255) - g)/300.
    db = (rint(0,255) - b)/300.
    for i in range(300):
        r,g,b = r+dr, g+dg, b+db
        draw.line((i,0,i,300), fill=(int(r),int(g),int(b)))

    f = cStringIO.StringIO()
    img.save(f, "PNG")

    print "Content-type: image/png\n"
    f.seek(0)
    print f.read()

if __name__ == "__main__":
    randgradient()

这段代码在内存中有一张图片,我试图让它成为一个tkinter背景,并带有这段代码:

from Tkinter import *
Admin=Tk()
image1 = PhotoImage(file="f.png")
w = image1.width()
h = image1.height()
Admin.geometry("%dx%d+0+0" % (w, h))
panel1 = Label(Admin, image=image1)
panel1.pack()
Admin.mainloop

我怎么做并不重要但是上面的方式不起作用所以我该如何去做呢?

1 个答案:

答案 0 :(得分:0)

您可以使用PIL的ImageTk模块将图像转换为Tk图像。请参阅PhotoImage课程示例。