Please bear with a Python/Tkinter newbie here.
I'm having trouble knowing when a widget will be updated on screen.
I've included a program that creates a window with a single button.
When that button is pressed it brings up a configuration window which
lets you pick the color of 'car' and 'truck'.
After the user chooses to update the color of car, the StringVar
which contains the color is updated and the color chooser closes. The
'car' button still displays with its original color, but when the config
window closes, it prints out a message indicating the car color was
updated. When the config window is opened again, the 'car' button is
the color which was picked.
The truck button operates the same except the widget parameter 'fg'
is updated when the StringVar is updated. In this case, the 'truck'
button changes color immediately. This is fine, except it requires that
the button be named so the 'fg' parameter can be changed: name['fg']=color.
Is it possible to make the un-named button update without closing and
reopening the window?
Thanks for any insight you can give me.
from Tkinter import *
import tkSimpleDialog
import tkColorChooser
#************************************************* ******************************
class base:
def __init__(self,parent):
self.main=Frame(parent)
self.main.pack()
Button(self.main,text="Config",bg='light blue',
width=20,height=8,command=self.conf).pack()
self.info={"car":'#FF0000',"truck":'#0000FF'}
def conf(self):
print 'Starting with car %s and truck%s'%(self.info['car'],
self.info['truck'])
UNIT_config(self.main,self.info)
#************************************************* *****************************
class UNIT_config(tkSimpleDialog.Dialog):
def __init__(self,parent,db):
self.paint=db
tkSimpleDialog.Dialog.__init__(self,parent)
def body(self,parent):
self.car_color=StringVar()
self.car_color.set(self.paint["car"])
self.truck_color=StringVar()
self.truck_color.set(self.paint["truck"])
Button(parent,text='car',fg=self.car_color.get(),
command=lambda a1=self.car_color,a2='car':
self.color_config(a1,a2)).grid(row=1)
self.b2=Button(parent,text="truck",fg=self.truck_c olor.get(),
command=lambda a1=self.truck_color,a2='truck':
self.color_config(a1,a2))
self.b2.grid(row=2)
def apply(self):
self.paint["car"]=self.car_color.get()
self.paint["truck"]=self.truck_color.get()
print 'Now car color', self.car_color.get()
print 'Now truck color', self.truck_color.get(),'\n**********'
def color_config(self,color_var,type):
old_color=color_var.get()
new_color=tkColorChooser.askcolor(old_color)
if new_color[0]:
red=hex(new_color[0][0])[2:]
green=hex(new_color[0][1])[2:]
blue=hex(new_color[0][2])[2:]
if len(red)==1: red='0'+red
if len(green)==1: green='0'+green
if len(blue)==1: blue='0'+blue
new_color_rgb='#'+red+green+blue
else: new_color_rgb=old_color
if type=='car':
self.car_color.set(new_color_rgb)
else:
self.truck_color.set(new_color_rgb)
self.b2['fg']=new_color_rgb
#************************************************* *****************************
root = Tk()
prop = base(root)
root.mainloop()