473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1337
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dean | last post by:
Hi All, I was checking out the demos for tk 8.4.4.0 and noticed that alot of them (e.g. the scrollbar) have the XP style. I also noticed that when I use Python 2.3 and a seperately installed...
3
by: bromden | last post by:
Are there any free Python database widgets which provide a link between a database and visual controls? (like those of org.netbeans.lib.sql and org.netbeans.lib.sql.models Java packages or...
0
by: Eric Williams | last post by:
Hello all, I'm working on a little application using PyQt. The main window is just a dialog with 2 progress bars and a few labels. In its original form, the dialog started, then ran a function...
1
by: Marc | last post by:
Hi all, After some research I've decided that my previous question (Confusing problem between Tkinter.Intvar...) was headed in the wrong direction. Partly because I think I have a greater...
2
by: Adonis | last post by:
I am creating some widgets by inheriting from Tkinter.Frame and populating the frame with whatever the widget will be then creating certain attributes/methods to be accessed later. My question is,...
2
by: William Gill | last post by:
I need to display a couple of labels and a checkbox from each entry in my database. Simple enough, but there are several hundred records, and I only want to display 5 or 10 at a time. Can this be...
4
by: KvS | last post by:
Hi all, I'm pretty new to (wx)Python so plz. don't shoot me if I've missed something obvious ;). I have a panel inside a frame, on which a Button and a StaticText is placed: self.panel =...
2
by: PT | last post by:
Hi, I have created a basic form that contains a menu and tab control and a widget that a directx is rendered too, if I remove the directx rendering all is well, but when it's rendering non of the...
2
by: nholtz | last post by:
Is there any way to delete a widget (window) from a Text widget, and then add it back to the Text, without re-creating the original widget. For example, I think I would like to do something like...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.