如何使用tkinter显示图像文件?

huangapple go评论62阅读模式
英文:

How can I display an image file with tkinter?

问题

我想创建一个基于Tkinter的Python程序,允许我打开一个图像文件并在Label小部件中显示它。但当我运行程序并点击按钮调用OpenFile()函数时,它不会显示图像。如何在Label小部件中显示图像?

谢谢!

我的程序看起来像这样:

import tkinter
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from PIL import Image, ImageTk

window = Tk() # 应用程序的主窗口
menu_bar = Menu(window) # 应用程序菜单栏

def OpenFile():
    "打开图像文件"
    try:
        file = filedialog.askopenfilename(initialdir="E:", filetypes=[("图像文件", (".png"))])
        image = Image.open(file)

        img = ImageTk.PhotoImage(image)
        #img.resize(50, 50)

        display = tkinter.Label(window)
        display.image = img
        display.pack()
        return file

    except FileNotFoundError:
        messagebox.showerror("文件未找到", "未找到所选文件。")

file_menu = Menu(menu_bar, tearoff=0)  # 添加一个"文件"菜单到菜单栏
file_menu.add_command(label="打开文件...", command=OpenFile)
file_menu.add_command(label="退出...", command=window.destroy)
menu_bar.add_cascade(label="文件", menu=file_menu)
window.config(menu=menu_bar)
window.mainloop()
英文:

I want to do a Tkinter-based python program which would allow me to open an image file and to display it in a Label widget. But when I run the program and I click the button to call the OpenFile() function, it does not display the image. How can I display the image in the Label widget ?

Thank you !

My program looks like this :

import tkinter
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from PIL import Image, ImageTk

window = Tk() # App main window
menu_bar = Menu(window) # App menu bar

def OpenFile():
  "Open an image"
  try:
    file = filedialog.askopenfilename(initialdir= "E:", filetypes= [("Image file", (".png"))])
    image = Image.open(file)

    img = ImageTk.PhotoImage(image)
    #img.resize(50, 50)

    display = tkinter.Label(window)
    display.image = img
    display.pack()
    return file

  except FileNotFoundError:
    messagebox.showerror("Unfound file", "The selected file was not found.")


file_menu = Menu(menu_bar, tearoff= 0)  # Add a "File" menu to menu_bar
file_menu.add_command(label= "Open file...", command= OpenFile)
file_menu.add_command(label= "Quit...", command= window.destroy)
menu_bar.add_cascade(label= "File", menu= file_menu)
window.config(menu= menu_bar)
window.mainloop()

答案1

得分: 1

img 存储包含您的图像的对象(将 filepath 替换为 file
然后我们创建一个带有该图像的标签。

英文:

Try this:

img = tk.PhotoImage(file="filepath")
tk.Label(window, image=img).pack()

img stores an object containing your image (replace filepath with file)
Then we create a lebel with the image.

> Let me know if you need help!

答案2

得分: 1

你忘记给tkinter.Labelimage选项赋值:

display = tkinter.Label(window, image=img) # 需要设置image选项

我建议创建标签一次,然后在函数内部更新其image选项:

...
def OpenFile():
  "打开一个图像"
  try:
    file = filedialog.askopenfilename(initialdir="", filetypes=[("图像文件", (".png"))])
    if file:
        img = ImageTk.PhotoImage(file=file)
        #img.resize(50, 50)

        #display = tkinter.Label(window, image=img)
        display.config(image=img)
        display.image = img
        return file

  except FileNotFoundError:
    messagebox.showerror("文件未找到", "未找到所选文件。")

display = tkinter.Label(window) # 创建标签一次
display.pack()
...
英文:

You forget to assign the image option of tkinter.Label:

display = tkinter.Label(window, image=img) # need to set the image option

I would suggest to create the label once and update its image option inside the function instead:

...
def OpenFile():
  "Open an image"
  try:
    file = filedialog.askopenfilename(initialdir= "", filetypes= [("Image file", (".png"))])
    if file:
        img = ImageTk.PhotoImage(file=file)
        #img.resize(50, 50)

        #display = tkinter.Label(window, image=img)
        display.config(image=img)
        display.image = img
        return file

  except FileNotFoundError:
    messagebox.showerror("Unfound file", "The selected file was not found.")

display = tkinter.Label(window) # create label once
display.pack()
...

huangapple
  • 本文由 发表于 2023年2月27日 02:03:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定