如何将JPEG图像插入python Tkinter窗口?

2024-06-06 22:59:19 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将JPEG图像插入Python2.7Tkinter窗口?下面的代码有什么问题?这个图像被称为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()

Tags: 代码图像imageimportbintitletkinterusr
1条回答
网友
1楼 · 发布于 2024-06-06 22:59:19

试试这个:

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 ModuleTkinter Label WidgetTkinter Pack Geometry Manager

相关问题 更多 >