Configuration Files and Tkinter--Possible?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Mar 4 16:27:53 CET 2009


On Wed, 04 Mar 2009 06:12:50 -0800, W. eWatson wrote:

> That's fine, but I think my problem boils down to one question. There
> seem to be two ways to communicate with a dialog (I mean a collection of
> widgets assembled in a window that requires the user enter various
> parameters, integers, strings, yes/no button, etc.): 1. a callback and
> 2. control variables. Which one should be used?

That's no two ways.  There are two ways to read/set the text in the 
`Entry`:  using `Entry`'s methods or by passing a `Variable` instance and 
use that object's methods.  The callback is used in both cases.

> To be more explicit, look at this code from your source above
> <http://effbot.org/tkinterbook/entry.htm>. (This is about the simplest
> "dialog" one can have.) :
> ======================start
> from Tkinter import *
> 
> master = Tk()
> e = Entry(master)
> e.pack()
> e.focus_set()
> def callback():
>      print e.get()
> b = Button(master, text="get", width=10, command=callback) b.pack()
> mainloop()
> =======================end
> Note that above this example, the author mentions: "You can also bind
> the entry widget to a StringVar instance, and set or get the entry text
> via that variable:
> 
> v = StringVar()
> e = Entry(master, textvariable=v)
> e.pack()
> 
> v.set("a default value")
> s = v.get()
> "

In this example the callback is missing.  Both operations, setting the 
text and getting it back, are performed without any user interaction 
directly one after another.  Which just demonstrate how to use 
`StringVar`.

> Why have two ways of doing this?

Convenience.  Setting a `Variable` that is used in a widget, 
automatically updates the display of the widget.  And `Variable`\s have a 
method to add callbacks that get called when the content changes.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list