color propagation in tkinter

A

Alan G Isaac

I'm a tkinter novice.

If I want to propagate changes to a font,
I can just use a named font.

What if I want to propagate color changes?
(E.g., a change in background color for
a number of widgets.)

Thanks,
Alan Isaac
 
M

Mike Driscoll

I'm a tkinter novice.

If I want to propagate changes to a font,
I can just use a named font.

What if I want to propagate color changes?
(E.g., a change in background color for
a number of widgets.)

Thanks,
Alan Isaac

One way would be to keep a list of the widgets that you want to be
able to change the color of, and then loop over the list and change
their respective colors as needed.

Mike
 
A

Alan G Isaac

One way would be to keep a list of the widgets that you want to be
able to change the color of, and then loop over the list and change
their respective colors as needed.


Yes, that's what I am currently doing.
But it seems that that is exactly the kind of thing
we are enabled to avoid with e.g., named fonts.
So I was hoping for an equivalent functionality
for colors. From your answer, I am guessing there
is none.

This is an example where different behavior by
StringVar could have been put to good use, I think.

Thanks,
Alan
 
T

Terry Reedy

Alan said:
Yes, that's what I am currently doing.
But it seems that that is exactly the kind of thing
we are enabled to avoid with e.g., named fonts.
So I was hoping for an equivalent functionality
for colors. From your answer, I am guessing there
is none.

This is an example where different behavior by
StringVar could have been put to good use, I think.

You could file a feature request on the tracker if this seems reasonable.
 
J

John Posner

Alan said:
Yes, that's what I am currently doing.
But it seems that that is exactly the kind of thing
we are enabled to avoid with e.g., named fonts.
So I was hoping for an equivalent functionality
for colors. From your answer, I am guessing there
is none.
Not sure this would help, but Tkinter supports the X Window System
"resources" facility, calling it the "option database". [1] So it's
pretty easy to create a subclass of Tkinter.Frame whose instances get a
user-configured background color, e.g. cyan:

class Aframe(Frame):
def __init__(self, master, **kwargs):
Frame.__init__(self, master, class_="Aframe", **kwargs)
...
root.option_add("*Aframe*background", "cyan")

Notes:

* The class name "Aframe" in the option_add() statement does not refer
to the Python subclass "Aframe". Rather, it refers to the value of
"class_=" in the __init__() method.

* The "class_=" keyword argument must appear in the call to the parent
class's __init__() method. You can't reconfigure an existing object,
e.g.: myobj.config(class_="Aframe").

* As far as I can tell, the "class_=" keyword argument is supported only
by the Frame widget initializer. You cannot, for example, use this
facility to specify the background color of a Tkinter.Label subclass. :=(

* This facility initializes a new widget, but cannot be used to
reconfigure an existing widget. [2]

Sounds useful, huh? :) Anyway, here's a proof-of-concept:

from Tkinter import *

class Aframe(Frame):
def __init__(self, master, **kwargs):
Frame.__init__(self, master, class_="Aframe", **kwargs)

class Bframe(Frame):
def __init__(self, master, **kwargs):
Frame.__init__(self, master, class_="Bframe", **kwargs)

root = Tk()
root.geometry("200x300")

# set backgrounds for different classes
root.option_add("*Aframe*background", "cyan")
root.option_add("*Bframe*background", "pink")

# overall frame
panel = Frame(root)
panel.pack(padx=25, pady=20, expand=YES, fill=BOTH)

# vertical padding factor
p = 10

# created colored frames
Aframe(panel).pack(pady=p, expand=YES, fill=BOTH)
Bframe(panel).pack(pady=p, expand=YES, fill=BOTH)
Aframe(panel).pack(pady=p, expand=YES, fill=BOTH)
Aframe(panel).pack(pady=p, expand=YES, fill=BOTH)
Aframe(panel).pack(pady=p, expand=YES, fill=BOTH)
Bframe(panel, relief=GROOVE, bd=4).pack(pady=p, expand=YES, fill=BOTH)

# go
root.mainloop()
This is an example where different behavior by
StringVar could have been put to good use, I think.
I don't follow this. Would you elaborate?


[1] http://infohost.nmt.edu/tcc/help/pubs/tkinter/option-database.html
[2] http://www.cs.man.ac.uk/~fellowsd/tcl/option-tutorial.html says
"Note that there is no automatic update of existing widgets when the
database is modified. If you want this, you will need to implement it
yourself."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top