简体   繁体   English

tkinter小部件的cnf参数

[英]cnf argument for tkinter widgets

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. 所以,我正在挖掘这里的代码和每个类(几乎)我在构造函数中看到一个参数cnf={} ,但除非我错过了它,否则没有明确说明cnf /期望包含什么。 Can anyone clear this up? 任何人都可以清除这个吗? At first I thought it was for the keywords passed to tkinter widgets, but kw is appropriately for that. 起初我认为这是传递给tkinter小部件的关键字,但kw适合于此。 So, I'm confused about what role cnf={} is playing.. as well as the extra=() argument(s). 所以,我对cnf={}正在扮演的角色以及extra=()参数感到困惑。

cnf must be a configuration dictionary mapping option names to values. cnf必须是将选项名称映射到值的配置字典。 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. 选项也可以传递(通过关键字),个别选项覆盖dict中的任何选项。 I verified these details with this experiment, run from IDLE. 我从这个实验验证了这些细节,从IDLE运行。 (If you run the below from a command line, add '-i' to the command line or root.mainloop() to the code.) (如果从命令行运行以下命令行,请将'-i'添加到命令行或将root.mainloop()添加到代码中。)

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()

The result (stacked vertically) is a blank red label, 2 red 'abc' labels, a blue 'abc' label, and a TypeError in IDLE's Shell. 结果(垂直堆叠)是一个空白的红色标签,2个红色的'abc'标签,一个蓝色的'abc'标签,以及IDLE的Shell中的TypeError。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM