Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 314
Pythonista Avatar asked Jan 19 '16 00:01

Pythonista


1 Answers

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

The result (stacked vertically) is a blank red label, 2 red 'abc' labels, a blue 'abc' label, and a TypeError in IDLE's Shell.

like image 84
Terry Jan Reedy Avatar answered Sep 19 '22 00:09

Terry Jan Reedy