cnf argument for tkinter widgets

2024/10/11 18:18:31

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

Answer

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.

https://en.xdnf.cn/q/69743.html

Related Q&A

python exceptions.UnicodeDecodeError: ascii codec cant decode byte 0xa7 in

I am using scrapy with python and I have this code in a python item piplinedef process_item(self, item, spider):import pdb; pdb.set_trace()ID = str(uuid.uuid5(uuid.NAMESPACE_DNS, item[link]))I got this…

Trace Bug which happends only sometimes in CI

I have a strange bug in python code which only happens sometimes in CI.We cant reproduce it.Where is the test code:response=self.admin_client.post(url, post) self.assertEqual(200, response.status_code,…

Limit neural network output to subset of trained classes

Is it possible to pass a vector to a trained neural network so it only chooses from a subset of the classes it was trained to recognize. For example, I have a network trained to recognize numbers and l…

Unable to fully remove border of PyQt QGraphicsView

I have tried calling self.setStyleSheet("background: transparent; border: transparent;") on a QGraphicsView, but it still leaves a 1 pixel border on the top edge. I have also tried replacing …

SqlAlchemy non persistent column

I am trying to define a model using sqlalchemy such that one of the columns i only need in memory for processing, i do not have a corresponding database column for that. I need it such that when i save…

Creating command line alias with python

I want to create command line aliases in one of my python scripts. Ive tried os.system(), subprocess.call() (with and without shell=True), and subprocess.Popen() but I had no luck with any of these me…

Remove unwanted lines in captcha text - opencv - python

I trying to get text from captcha image using opencv. Problem is text are masked with noise and it is complex to process with those horizontal line/noise.Original imageMy processed image :not sure how …

Double Summation in Python

I am trying to write a code to conduct a double summation (see pic) in which; M is the subjects, N is the Trials, Yijt is the measured wave form data (3d array)so far I have; Given Y is the data arra…

psycopg, double and single quotes insert

I ran into problems, while trying to insert to database:ur_psql.execute("""insert into smth(data, filedate, filedby)"""""" values(%s, NOW(), %s)""…

How to debug Python 2.7 code with VS Code?

For work I have to use Python 2.7. But when I use the "debug my python file" function in VS Code, I get an error. Even with a simple program, like : print()