如何在python2.7中使用tk在gui上显示图像
如果可能的话,我需要能够在以gui形式运行python代码时显示图像。如果你知道我试过的答案的话,我还需要在代码中输入文件夹名和文件名吗?pil所做的只是将图像保存到一个我希望在gui上显示的文件夹中 tk的代码(如有必要)如何在python2.7中使用tk在gui上显示图像,python,image,python-2.7,tkinter,tk,Python,Image,Python 2.7,Tkinter,Tk,如果可能的话,我需要能够在以gui形式运行python代码时显示图像。如果你知道我试过的答案的话,我还需要在代码中输入文件夹名和文件名吗?pil所做的只是将图像保存到一个我希望在gui上显示的文件夹中 tk的代码(如有必要) from Tkinter import * root = Tk() toolbar = Frame(root) # All the buttons in my program b = Button(toolbar, text="Home", width=9) b.pa
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)
您好如果您希望在tk画布中显示图像,则图像必须是PPM/PGM或GIF: 但是,如果您希望加载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加载图像,然后制作画布并将图像放到画布上。您不需要使用画布;你也可以使用标签小部件。我认为这是最“干净”的方式。我的观点是,说某人需要以某种方式做某事意味着这是唯一的方式,但在这种情况下,这不是唯一的方式。