如何向 Python Tkinter 窗口插入 JPEG 图像?
Python 提供了**Pillow** (PIL) 包,可在 tkinter 应用程序中支持、处理和显示图像。Tkinter 应用程序通常支持图像文件,例如 ppm、png 和 gif。
假设我们要在应用程序中嵌入和显示 JPEG 或 JPG 图像。
Tkinter Label 小部件通常用于在窗口上显示文本或图像,因此通过传递 img 值,我们可以在窗口中显示 JPEG 图像。
示例
#Import required libraries
from tkinter import *
from PIL import ImageTk, Image
#Create an instance of tkinter window
win =Tk()
#Define the geometry of the window
win.geometry("650x400")
#Initialize the file name in a variable
path = "file.jpg"
#Create an object of tkinter ImageTk
img = ImageTk.PhotoImage(Image.open(path))
#Create a Label Widget to display the text or Image
label = tk.Label(win, image = img)
label.pack(fill = "both", expand = "yes")
win.mainloop()输出
该代码将显示作为 Label 小部件中的图像值传递的 JPEG 图像。

广告
数据结构
联网
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP