如何在python2.7上使用tk显示gui上的图像
问题描述:
如果可能的话,我需要在能够以gui形式运行python代码时显示图像。我也必须在代码中输入文件夹名称和文件名如果你知道答案我试过pil所有它所做的就是将图像保存到一个文件夹中我想要这个在gui上如何在python2.7上使用tk显示gui上的图像
代码为tk if这是必要的
from Tkinter import *
root = Tk()
toolbar = Frame(root)
# All the buttons in my program
b = Button(toolbar, text="Home", width=9)
b.pack(side=LEFT, padx=2, pady=2)
b = Button(toolbar, text="About-us", width=9)
b.pack(side=LEFT, padx=2, pady=2)
b = Button(toolbar, text="Contact-us", width=9)
b.pack(side=LEFT, padx=2, pady=2)
b = Button(toolbar, text="Travelling", width=9)
b.pack(side=LEFT, padx=2, pady=2)
toolbar.pack(side=TOP, fill=X)
# All the labels in my program
o = Label(root,text="Frank Anne",font =("Italic",18,"bold"))
o.pack()
toolbar1 = Frame(root)
o=Label(root,)
o.pack()
o=Label(root,text ="Age: 27")
o.pack()
o=Label(root,text ="Address: 71 strawberry lane, JU8 8TY")
o.pack()
toolbar1.pack(side=BOTTOM, fill=X)
mainloop()
答
如果我明白,你只是想显示使用Tkinter的图像?
要做到这一点,您可以使用充满图像的画布小部件。
下面是一个例子:
can = Canvas(window, width=160, height=160, bg='white')
pic = PhotoImage(file='picture.jpg')
item = can.create_image(80, 80, image=pic)
答
您好,如果您希望显示它需要一个PPM/PGM或GIF传统知识画布内的图像: discussion from python.org
但是,如果你希望加载一个PPM,PGM或GIF:
import Tkinter as tk
root = tk.Tk()
root.title("display a website image")
photo = tk.PhotoImage(file= r"C:\Some\Local\location\smile.gif")
cv = tk.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(10, 10, image=photo, anchor='nw')
root.mainloop()
这就形成了一个框架,装载使用tk.PhotoImage的图像,然后使一个画布和将图像上画布。
您不需要使用画布;你也可以使用标签小部件。 – 2013-05-09 17:41:13
我认为这是做到“最干净”的方式。 – gaetanm 2013-07-18 10:36:53
我的观点是,要求某人以某种方式做某事意味着这是唯一的方式,但在这种情况下,这不是唯一的方法。 – 2013-07-18 10:44:26