将数据设置为 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
我如何做到这一点并不重要,但上面的方法不起作用,所以如何我会去做吗?
so i found this code:
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()
this code has a picture in the memory and im trying to make it a tkinter background with posibly this bit of code:
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
it doesn't really matter how i do it but the way just above does not work so how would i go about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 PIL 的 ImageTk 模块将图像转换为 Tk 图像。请参阅 PhotoImage 类示例。
You can convert an image to a Tk image using PIL's ImageTk module. See the PhotoImage class examples.