如何在python2.7中使用tk在gui上显示图像 [英] how can you display an image on a gui using tk in python2.7

查看:1201
本文介绍了如何在python2.7中使用tk在gui上显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能的话,当我以gui形式运行python代码时,我需要能够显示图像。我也必须在代码中键入文件夹名称和文件名称,如果你知道答案我试过它所做的就是将图像保存到文件夹中我想要这个gui

i need away to be able to show an image when i run the python code in its gui form if possible. Also do i have to type in the folder name and file name in the code if you know the answer i tried pil all it did was save the image into a folder i want this on the gui

tk的代码(如果有必要)

the code for the tk if it is necessary

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()


推荐答案

您好,如果您希望在tk Canvas中显示图像,则需要将其设为PPM / PGM或GIF:
来自python.org的讨论

Hello if your looking to show an image inside a tk Canvas it needs to be a PPM/PGM or GIF: discussion from python.org

但是,如果您要加载PPM,PGM或GIF:

However if your looking to load a PPM, PGM or 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加载图像,然后制作画布并将图像放到画布上。

This creates a frame, loads the image using tk.PhotoImage, then makes a canvas and puts the image onto the canvas.

这篇关于如何在python2.7中使用tk在gui上显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆