tkinter 小部件的 cnf 参数 [英] cnf argument for tkinter widgets
问题描述
所以,我正在挖掘代码 here 和在每个类中(几乎)我看到构造函数的参数 cnf={}
,但除非我错过了它,否则没有明确说明 cnf 是什么/预期包含装有.任何人都可以解决这个问题吗?起初我以为是传递给 tkinter 小部件的关键字,但 kw 适合于此.所以,我对 cnf={}
所扮演的角色感到困惑......以及 extra=()
参数.
So, I'm digging through the code here and in every class (almost) I see an argument cnf={}
to the constructor, but unless I've missed it, it is not explicitly stated what cnf is / expected to contain. Can anyone clear this up? At first I thought it was for the keywords passed to tkinter widgets, but kw is appropriately for that. So, I'm confused about what role cnf={}
is playing.. as well as the extra=()
argument(s).
推荐答案
cnf 必须是一个配置字典,将选项名称映射到值.它可以按位置或按名称传递(但不能同时通过!).当多个小部件具有一组通用选项时,这非常有用.也可以传递选项(通过关键字),并且单个选项会覆盖字典中的任何选项.我通过这个实验验证了这些细节,从 IDLE 运行.(如果您从命令行运行以下命令,请将-i"添加到命令行或将 root.mainloop() 添加到代码中.)
cnf must be a configuration dictionary mapping option names to values. It can be passed by position or by name (but not both!). This is very useful when several widgets have a common set of options. Options can also be passed (by keyword), and individual options override any in the dict. I verified these details with this experiment, run from IDLE. (If you run the below from a command line, add '-i' to the command line or root.mainloop() to the code.)
import tkinter as tk
root = tk.Tk()
d = {'bg': 'red'}
tk.Label(root, d).pack()
tk.Label(root, d, text='abc').pack()
tk.Label(root, cnf=d, text='abc').pack()
tk.Label(root, d, text='abc', bg='blue').pack()
tk.Label(root, d, cnf=d, text='abc').pack()
结果(垂直堆叠)是一个空白的红色标签、2 个红色的abc"标签、一个蓝色的abc"标签和 IDLE 的 Shell 中的 TypeError.
The result (stacked vertically) is a blank red label, 2 red 'abc' labels, a blue 'abc' label, and a TypeError in IDLE's Shell.
这篇关于tkinter 小部件的 cnf 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!