如何将JPEG图像插入python Tkinter窗口? [英] How do I insert a JPEG image into a python Tkinter window?

查看:1148
本文介绍了如何将JPEG图像插入python Tkinter窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将JPEG图像插入Python 2.7 Tkinter窗口?以下代码有什么问题?图像名为 Aaron.jpg

How do I insert a JPEG image into a Python 2.7 Tkinter window? What is wrong with the following code? The image is called Aaron.jpg.

#!/usr/bin/python

import Image
import Tkinter
window = Tkinter.Tk()

window.title("Join")
window.geometry("300x300")
window.configure(background='grey')

imageFile = "Aaron.jpg"

window.im1 = Image.open(imageFile)


raw_input()
window.mainloop()


推荐答案

试试这个:

import Tkinter as tk
from PIL import ImageTk, Image

#This creates the main window of an application
window = tk.Tk()
window.title("Join")
window.geometry("300x300")
window.configure(background='grey')

path = "Aaron.jpg"

#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
img = ImageTk.PhotoImage(Image.open(path))

#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
panel = tk.Label(window, image = img)

#The Pack geometry manager packs widgets in rows or columns.
panel.pack(side = "bottom", fill = "both", expand = "yes")

#Start the GUI
window.mainloop()

相关文档: ImageTk模块 Tkinter Label Widget Tkinter Pack Geometry Manager

这篇关于如何将JPEG图像插入python Tkinter窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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