PhotoImage -- 省略选项名称 [英] PhotoImage -- omitting option name

查看:28
本文介绍了PhotoImage -- 省略选项名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到这个后,PhotoImage 没有显示相关的图像有了它我想知道为什么省略选项类型不会引发错误而不是只是不显示图像?

After seeing this, PhotoImage not showing up the image associated with it I wondered why omitting the type of option does not throws an error instead of just not showing up the image?

此代码不显示图像而不会引发任何错误

This code does not show the image without throwing any error

import tkinter as tk
root = tk.Tk()

image1 = tk.PhotoImage("ban.gif")
tk.Label(root,image=image1).pack()
tk.Label(root, text="some string here").pack()

root.mainloop()

但是这个很好用

import tkinter as tk
root = tk.Tk()

image1 = tk.PhotoImage(file="ban.gif")
tk.Label(root,image=image1).pack()
tk.Label(root, text="some string here").pack()

root.mainloop()

在 effbot 上它什么也没说,所以我检查了 tcl 手册页的 正在创建照片,但仍然无法找到为什么会这样.

On effbot it doesn't say anything about it so I checked tcl man page for creating photos but still can not find why it behaves like this.

另外,如果这两个是重复的,请告诉我,以便我删除/关闭投票.

Also, if those two are duplicate ones, let me know so I will delete/close vote this one.

推荐答案

当您在 Python 中指定带有命名参数的函数时,这些命名参数会按特定顺序出现.如果您在定义这些参数时未提供名称,则会按照参数在函数定义中出现的顺序应用它们.

When you specify a function with named arguments in python, those named arguments appear in a specific order. If you do not supply a name when defining these arguments, they are applied in the order that the arguments appear in the function definition.

在 PhotoImage 的情况下,第一个关键字参数是图像的名称,而不是文件的路径.所以,PhotoImage("ban.gif")PhotoImage(name="ban.gif") 是一样的.它不会引发错误,因为ban.gif"是一个有效名称,并且在某些用例中,您希望在不引用文件的情况下创建图像.

In the case of PhotoImage, the first keyword argument is for the name of the image, not a path to a file. So, PhotoImage("ban.gif") is the same as doing PhotoImage(name="ban.gif"). It doesn't throw an error because "ban.gif" is a valid name, and there are use cases where you want to create an image without referencing a file.

这篇关于PhotoImage -- 省略选项名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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