469,304 Members | 1,759 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

Updating Widgets

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()
Jul 18 '05 #1
2 1280
SWright wrote:
Is it possible to make the un-named button update without closing and
reopening the window?


No, not if you insist on not giving the button a name so that you can
refer to it later.

The only widget options that can automatically track changes to a
variable are those with "variable" in their names, for example the
textvariable= option on Labels. Even in the cases where you can use a
variable, it doesn't really buy you much to be able to call .set() on
the variable rather than .config() on the widget - you have to keep a
reference to SOMETHING in either case. I think the only places in
Tkinter where use of a Tcl variable is unavoidably required are the
Radiobutton and Checkbox widgets, and the corresponding menu items.
Jason Harper
Jul 18 '05 #2
Thanks very much for the explanation. The actual program I'm working on
has a lot of buttons so I was looking at different ways to control them.
This will at least keep me from spending any more time on this path.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dean | last post: by
3 posts views Thread by bromden | last post: by
reply views Thread by Eric Williams | last post: by
2 posts views Thread by Adonis | last post: by
2 posts views Thread by PT | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.