tkinter 不通过函数打开图像 [英] tkinter doesn't open image via function

查看:73
本文介绍了tkinter 不通过函数打开图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tkinter 不打开图像.我们可以问错了,我们需要帮助.我需要它来通过菜单打开图像.一定要使用 pil,因为图像可以是任何东西.语法上没有错误.谢谢=)

Tkinter doesn't open the image. We can ask the opening incorrectly, we need help. I need it to open the image through the menu. be sure to use pil, as the image can be anything. There are no errors in the syntax. Thank you = )

from tkinter import Tk, Frame, Menu, Canvas, PhotoImage
import easygui
from PIL import Image, ImageFilter, ImageTk

def input_file():
    a = easygui.fileopenbox(filetypes=["*.jpg"])
    original = Image.open(a)
    original = original.resize((799, 799), Image.ANTIALIAS)
    photoimg = ImageTk.PhotoImage(original)
    canvas = Canvas(root, width=799, height=799)
    imagesprite = canvas.create_image(10, 10,anchor='nw', image=photoimg)
    canvas.pack()
    return (imagesprite)

root = Tk()
root.title("Sputnikeca")
#root.iconbitmap('путь к иконке')
root.geometry("800x800+0+0")

my_menu = Menu(root)
root.config(menu=my_menu)

# Create a menu item

file_menu = Menu(my_menu)
my_menu.add_cascade(label = "Файл", menu=file_menu)
file_menu.add_command(label = "Импорт...", command=input_file())
file_menu.add_separator()
file_menu.add_command(label = "Выход", command=root.quit)

root.mainloop()

推荐答案

以下是解决问题的方法:

Here is what you have to do to solve the issue:

def input_file():
    global photoimg #keeping a reference
    a = easygui.fileopenbox(filetypes=["*.jpg"])
    original = Image.open(a).resize((799, 799), Image.ANTIALIAS) #calling it all in one line
    photoimg = ImageTk.PhotoImage(original)
    canvas = Canvas(root, width=799, height=799)
    imagesprite = canvas.create_image(10, 10,anchor='nw', image=photoimg)
    canvas.pack()
    return imagesprite

然后删除函数周围的 () :

and then later remove the () around your function:

file_menu.add_command(label = "Импорт...", command=input_file)

正在做什么?

  • 在第一组代码中,我保留了对图像的引用,因此图像不会被 python 垃圾收集.您可以通过在函数顶部说 imagesprite.image = photoimgglobal photoimg 来实现.我还在打开图像的同一行中调整了图像的大小,以减少代码.

  • In the first set of code im keeping a reference to the image so the image is not garbage collected by python. You can do so either by saying imagesprite.image = photoimg or global photoimg on top of the function. I also resized the image in the same line that I opened the image, to reduce codes.

在第二组代码中,我只是删除了 () 以便在选择菜单项之前不会调用(调用)该函数.

And in the second set of codes, im just removing () so that the function is not called(invoked) before choosing the menu item.

而且 tkinter 本身也有一个 filedialogbox 就像你的 easygui.fileopenbox(filetypes=["*.jpg"]),阅读一些文档这里

And also tkinter itself has a filedialogbox that works like your easygui.fileopenbox(filetypes=["*.jpg"]), read some docs here

from tkinter import filedialog

a = filedialog.askopenfilename(title='Choose a file',initialdir='C:/',filetypes=(('All Files','*.*'),("JPEG 
Files",'*.jpeg')))

希望这能帮助您解决错误,如有任何疑问,请告诉我.

Hope this helped you solve the error, do let me know if any doubts.

干杯

这篇关于tkinter 不通过函数打开图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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