Brill IanT | 2 May 18:41 2005
Picon

Problem looping

Hi all!
 
I was wondering if anyone could help me out.
I wrote a little alarm clock program in Python, and now want to incorporate it into a Tkinter frame. It's all going quite well, but this is my first experience with Tk and I've hit a bit of a wall. One of the button's commands refers to a method (called 'act') which has a while loop within it (keeps track of the time and ought to display it in the Tk frame). But the problem is the while loop is interupting the root mainloop, so the labels will not update, and freezes until the while loop is over.
Does anyone have any suggestions to get round this?
 
Here's the program:
 
import time, winsound
from Tkinter import *
 
class App:
    def __init__(self,master):
        self.master = master
        frame = Frame(master,bd=6,relief=RIDGE)
        frame.pack()
        frame2 = Frame(master)
        frame2.pack()
 
        self.list = ["9","8","7","6","5","4","3","2","1","0"]
        self.stringv = StringVar()
        self.stringv.set("0")
        self.stringv2 = StringVar()
        self.stringv2.set("0")
        self.stringv3 = StringVar()
        self.stringv3.set("0")
        self.stringv4 = StringVar()
        self.stringv4.set("0")
 
        self.lab1 = Label(frame,font=("helvica",22),bg="red",
                  textvariable=self.stringv).pack(side=LEFT)
        self.b1 = Button(frame2,text="H",width=3,command=self.change)
        self.b1.pack(side=LEFT)
 
        self.lab2 = Label(frame,font=("helvica",22),bg="red",
                  textvariable=self.stringv2).pack(side=LEFT)
        self.b2 = Button(frame2,text="h",width=3,command=self.change2)
        self.b2.pack(side=LEFT)
 
        self.lab3 = Label(frame,text=":",font=("helvica",22),
                          bg="red",).pack(side=LEFT)
 
        self.lab4 = Label(frame,font=("helvica",22),bg="red",
                  textvariable=self.stringv3).pack(side=LEFT)
        self.b3 = Button(frame2,text="M",width=3,command=self.change3)
        self.b3.pack(side=LEFT)
 
        self.lab5 = Label(frame,font=("helvica",22),bg="red",
                  textvariable=self.stringv4).pack(side=LEFT)
        self.b4 = Button(frame2,text="m",width=3,command=self.change4)
        self.b4.pack(side=LEFT)
 
        self.set_button = Button(master,text="Set",width=20,
                                 command=self.act).pack()
       
    def change(self):
        if int(self.stringv2.get()) < 3:
            self.list1 = self.list[7:]
            i = self.list1.index(self.stringv.get())
            self.stringv.set(self.list1[i-1])
        else:
             self.list1 = self.list[8:]
            i = self.list1.index(self.stringv.get())
            self.stringv.set(self.list1[i-1])
 
    def change2(self):
        self.list2 = self.list[6:]
        if self.stringv.get() == "2":
            i = self.list2.index(self.stringv2.get())
            self.stringv2.set(self.list2[i-1])
        else:  
            i = self.list.index(self.stringv2.get())
            self.stringv2.set(self.list[i-1])
 
    def change3(self):
        self.list3 = self.list[4:]
        i = self.list3.index(self.stringv3.get())
        self.stringv3.set(self.list3[i-1])
 
    def change4(self):
        i = self.list.index(self.stringv4.get())
        self.stringv4.set(self.list[i-1])
 
    def act(self):
        self.ring = self.stringv.get()+self.stringv2.get()+":"\
                    +self.stringv3.get()+self.stringv4.get()
        self.track = [None]
        self.c = self.clock()
        self.stringv.set(self.c[0])
        self.stringv2.set(self.c[1])
        self.stringv3.set(self.c[3])
        self.stringv4.set(self.c[4])
 
        while 1:
            self.c = self.clock()   
            if self.c != self.ring:
                if self.c == self.track[-1]: pass
                else:
                    self.stringv.set(self.c[0])
                    self.stringv2.set(self.c[1])
                     self.stringv3.set(self.c[3])
                    self.stringv4.set(self.c[4])
                    self.track.append(self.c)
            else: winsound.PlaySound("rooster.wav", winsound.SND_LOOP)
           
    def clock(self):
        t1 = time.localtime()[3:5]
        if t1[0] in range(10): tm1 = '0'+ str(t1[0])
        else: tm1 = str(t1[0])
 
        if t1[1] in range(10): tm2 = '0'+ str(t1[1])
        else: tm2 = str(t1[1])
 
        tm3 = tm1 + ":" + tm2
        return tm3
 
if __name__ == "__main__":
    root=Tk()
    root.title("")
    root.geometry("100x97+300+200")
    App(root)
    root.mainloop()
 
Thanks in advance.

Send instant messages to your online friends http://uk.messenger.yahoo.com

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss <at> python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
Cameron Laird | 2 May 18:44 2005
Picon

Re: Problem looping

On Mon, May 02, 2005 at 05:41:50PM +0100, Brill IanT wrote:
			.
			.
			.
> I was wondering if anyone could help me out. 
> I wrote a little alarm clock program in Python, and now want to incorporate it into a Tkinter frame. It's all
going quite well, but this is my first experience with Tk and I've hit a bit of a wall. One of the button's
commands refers to a method (called 'act') which has a while loop within it (keeps track of the time and
ought to display it in the Tk frame). But the problem is the while loop is interupting the root mainloop, so
the labels will not update, and freezes until the while loop is over. 
			.
			.
			.
Short answer:  <URL: http://wiki.tcl.tk/1526 >.

Do you want help translating this into Tkinter?
Harlin Seritt | 3 May 12:38 2005
Picon

Re: Tkinter-discuss Digest, Vol 15, Issue 1

Hi Ian,
 
I had some trouble doing something like this when I was trying to write a balloon widget. I was using while, threading, etc... Try changing your act() method to this:
 
    def act(self):
        self.ring = self.stringv.get()+self.stringv2.get()+":"\
                    +self.stringv3.get()+self.stringv4.get()
        self.track = [None]
        self.c = self.clock()
        self.stringv.set(self.c[0])
        self.stringv2.set(self.c[1])
        self.stringv3.set(self.c[3])
        self.stringv4.set(self.c[4])
 
         
        self.c = self.clock()   
 &n bsp;      if self.c != self.ring:
            if self.c == self.track[-1]: pass
            else:
                self.stringv.set(self.c[0])
                self.stringv2.set(self.c[1])
                self.stringv3.set(self.c[3])
                self.stringv4.set(self.c[4])
                self.track.append(self.c)
        else: winsound.PlaySound("rooster.wav", winsound.SN D_LOOP)
       
        self.master.after(1000, self.act)
 
Tk().after(milliseconds, method/function to call) is great for this sort of thing.
 
Good luck!
 
Harlin Seritt


tkinter-discuss-request <at> python.org wrote:
Send Tkinter-discuss mailing list submissions to
tkinter-discuss <at> python.org

To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/tkinter-discuss
or, via email, send a message with subject or body 'help' to
tkinter-discuss-request <at> python.org

You can reach the person managing the list at
tkinter-discuss-owner <at> python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tkinter-discuss digest..."


Today's Topics:

1. Problem looping (Brill IanT)
2. Re: Problem looping (Cameron Laird)


----------------------------------------------------------------------

Message: 1
Date: Mon, 2 May 2005 17:41:50 +0100 (BST)
From: Brill IanT
Subject: [Tkinter-discuss] Problem looping
To: tkinter-discuss <at> python.org
Message-ID: <20050502164151.17812.qmail <at> web26606.mail.ukl.yahoo.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi all!

I was wondering if anyone could help me out.
I wrote a little alarm clock program in Python, and now want to incorporate it into a Tkinter frame. It's all going quite well, but this is my first experience with Tk and I've hit a bit of a wall. One of the button's commands refers to a method (called 'act') which has a while loop within it (keeps track of the time and ought to display it in the Tk frame). But the problem is the while loop is interupting the root mainloop, so the labels will not update, and freezes until the while loop is over.
Does anyone have any suggestions to get round this?

Here's the program:

import time, winsound
from Tkinter import *

class App:
def __init__ (self,master):
self.master = master
frame = Frame(master,bd=6,relief=RIDGE)
frame.pack()
frame2 = Frame(master)
frame2.pack()

self.list = ["9","8","7","6","5","4","3","2","1","0"]
self.stringv = StringVar()
self.stringv.set("0")
self.stringv2 = StringVar()
self.stringv2.set("0")
self.stringv3 = StringVar()
self.stringv3.set("0")
self.stringv4 = StringVar()
self.stringv4.set("0")

self.lab1 = Label(frame,font=("helvica",22),bg="red",
textvariable=self.stringv).pack(side=LEFT)
self.b1 = Button(frame2,text="H",width=3,command=self.change)
self.b1.pack(side=LEFT)

self.lab2 = Label(frame,font=("helvica",22),bg="red",
textvariable=self.stringv2).pack(side=LEFT)
self.b2 = Button(frame2,text="h",width=3,command=self.change2)
self.b2.pack(side=LEFT)

self.lab3 = Label(frame,text=":",font=("helvica",22),
bg="red",).pack(side=LEFT)

self.lab4 = Label(frame ,font=("helvica",22),bg="red",
textvariable=self.stringv3).pack(side=LEFT)
self.b3 = Button(frame2,text="M",width=3,command=self.change3)
self.b3.pack(side=LEFT)

self.lab5 = Label(frame,font=("helvica",22),bg="red",
textvariable=self.stringv4).pack(side=LEFT)
self.b4 = Button(frame2,text="m",width=3,command=self.change4)
self.b4.pack(side=LEFT)

self.set_button = Button(master,text="Set",width=20,
command=self.act).pack()

def change(self):
if int(self.stringv2.get()) < 3:
self.list1 = self.list[7:]
i = self.list1.index(self.stringv.get())
self.stringv.set(self.list1[i-1])
else:
self.list1 = self.list[8:]
i = self.list1.index(self.stringv.get())
self.stringv.set(self.list1[i-1])

def change2(self):
self.list2 = self.list[6:]
if self.stringv.get() == "2":
i = self.list2.index(self.stringv2.get())
self.stringv2.set(self.list2[i-1])
else:
i = self.list.index(self.stringv2.get())
self.s tringv2.set(self.list[i-1])

def change3(self):
self.list3 = self.list[4:]
i = self.list3.index(self.stringv3.get())
self.stringv3.set(self.list3[i-1])

def change4(self):
i = self.list.index(self.stringv4.get())
self.stringv4.set(self.list[i-1])

def act(self):
self.ring = self.stringv.get()+self.stringv2.get()+":"\
+self.stringv3.get()+self.stringv4.get()
self.track = [None]
self.c = self.clock()
self.stringv.set(self.c[0])
self.stringv2.set(self.c[1])
self.stringv3.set(self.c[3])
self.stringv4.set(self.c[4])

while 1:
self.c = self.clock()
if self.c != self.ring:
if self.c == self.track[-1]: pass
else:
self.stringv.set(self.c[0])
self.stringv2.set(self.c[1])
self.stringv3.set(self.c[3])
self.stringv4.set(self.c[4])
self.track.append(self.c)
else: winsound.PlaySound("rooster.wav", winsound.SND_LOOP)

def clock(self):
t1 = time.localtime()[3:5]
if t1[0] in range(10): tm1 = '0'+ str(t1[0])
else: tm1 = str(t1[0])

if t1[1] in range(10): tm2 = '0'+ str(t1[1])
else: tm2 = str(t1[1])

tm3 = tm1 + ":" + tm2
return tm3

if __name__ == "__main__":
root=Tk()
root.title("")
root.geometry("100x97+300+200")
App(root)
root.mainloop()


Thanks in advance.


Send instant messages to your online friends http://uk.messenger.yahoo.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20050502/77610d5d/attachment.html

------------------------------

Message: 2
Date: Mon, 2 May 2005 16:44:54 +0000
From: Cameron Laird
Subject: Re: [Tkinter-discuss] Problem looping
To: Brill IanT
Cc: tkinter-discuss <at> python.org
Message-ID: <20050502164454.GA15435 <at> lairds.us>
Content-Type: text/plain; charset=us-ascii

On Mon, May 0 2, 2005 at 05:41:50PM +0100, Brill IanT wrote:
.
.
.
> I was wondering if ! anyone could help me out.
> I wrote a little alarm clock program in Python, and now want to incorporate it into a Tkinter frame. It's all going quite well, but this is my first experience with Tk and I've hit a bit of a wall. One of the button's commands refers to a method (called 'act') which has a while loop within it (keeps track of the time and ought to display it in the Tk frame). But the problem is the while loop is interupting the root mainloop, so the labels will not update, and freezes until the while loop is over.
.
.
.
Short answer: .

Do you want help translating this into Tkinter?


------------------------------

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss <at> python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss


End of Tkinter-discus s Digest, Vol 15, Issue 1
**********************************************

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss <at> python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
Cameron Laird | 3 May 17:40 2005
Picon

Re: Tkinter-discuss Digest, Vol 15, Issue 1

On Tue, May 03, 2005 at 03:38:23AM -0700, Harlin Seritt wrote:
			.
			.
			.
> I had some trouble doing something like this when I was trying to write a balloon widget. I was using while,
threading, etc... Try changing your act() method to this:
>  
>     def act(self):
			.
			.
			.
>         self.master.after(1000, self.act)
>  
> Tk().after(milliseconds, method/function to call) is great for this sort of thing.
			.
			.
			.
Seconded:  while there are a number of ways to implement concurrency
in Tkinter, this "self-scheduling" partition into little event-chained
segments is the one I generally consider most apt.
Vania Smrkovski | 4 May 17:20 2005

Tkinter and threading

Hello,
  New to this group, and I have not found much in the way of documentation for advanced uses of Tkinter, yet.  So
far, Tk3k is the most advanced resource I have found yet.
  I am trying to implement threading in my Tkinter app.  I have managed to get a button to fire an FTP download
into one of my text areas, but to get rid of the delay on the GUI while the FTP process was being handled, I
created a thread in the button\'s command handler.  

  On my Linux box, the below handler works as I expected.  If I do not have the host, username and password
defined, I get a showerror() pop telling the user of the problem.  
  However, on my PC, I get a beep, but no pop.  

  The problem appears to be with calling a Tkinter method from within a thread.  When I pop a showerror()
command outside of the thread, I get exactly what I ask for.  But, as you can see in the exception handler, my
PC gets the PRINT statement, but no error dialog.

Here is a snippet from one of the class methods:
def HandleFTP(self):
	def AppendLine(data):
		self.UI.T.insert(END, data + \"\\n\")
	def threadcode():
		try:
			ftp = ftplib.FTP(self.UI.Host)
			ftp.login(self.UI.Username,self.UI.Password)
			ftp.cwd(\'/www/\')
			filename = \'admin.php\'
			ftp.retrbinary(\'RETR \' +filename, AppendLine)
			#ftp.storbinary(\'STOR \' +filename, open(filename, \'rb\'))
		except AttributeError:
			print \"Missing Configuration\", \"To use FTP, add the following to \" + \\
			\"your config.ini file\\n\\n\" + \\
			\"[FTP]\\n\" + \\
			\"Host=ftp.wherever.com\\n\" + \\
			\"Username=[yourusername]\\n\" + \\
			\"Password=[yourpassword]\"
			showerror(\"Missing Configuration\", \"To use FTP, add the following to \" + \\
			\"your config.ini file\\n\\n\" + \\
			\"[FTP]\\n\" + \\
			\"Host=ftp.wherever.com\\n\" + \\
			\"Username=[yourusername]\\n\" + \\
			\"Password=[yourpassword]\")
	t = threading.Thread(target=threadcode, name=\"ftpthread\")
	t.setDaemon(1)
	t.start()

  Am I missing something, or is there some specific approach required to do threading?  In all other ways, the
theading works exactly as expected.  That is, when I provide the host, username and password, I get my
button and UI fully functional with no delays, and then the text field gets populated.  

Thanks,
_____________________
Vania Smrkovski
www.pandorasdream.com
Martin Franklin | 5 May 08:35 2005
Picon

Re: Tkinter and threading

Vania Smrkovski wrote:
> Hello,
>   New to this group, and I have not found much in the way of documentation for advanced uses of Tkinter, yet.  So
far, Tk3k is the most advanced resource I have found yet.
>   I am trying to implement threading in my Tkinter app.  I have managed to get a button to fire an FTP download
into one of my text areas, but to get rid of the delay on the GUI while the FTP process was being handled, I
created a thread in the button\'s command handler.  
> 
>   On my Linux box, the below handler works as I expected.  If I do not have the host, username and password
defined, I get a showerror() pop telling the user of the problem.  
>   However, on my PC, I get a beep, but no pop.  
> 
>   The problem appears to be with calling a Tkinter method from within a thread.  When I pop a showerror()
command outside of the thread, I get exactly what I ask for.  But, as you can see in the exception handler, my
PC gets the PRINT statement, but no error dialog.
> 
> Here is a snippet from one of the class methods:
> def HandleFTP(self):
> 	def AppendLine(data):
> 		self.UI.T.insert(END, data + \"\\n\")
> 	def threadcode():
> 		try:
> 			ftp = ftplib.FTP(self.UI.Host)
> 			ftp.login(self.UI.Username,self.UI.Password)
> 			ftp.cwd(\'/www/\')
> 			filename = \'admin.php\'
> 			ftp.retrbinary(\'RETR \' +filename, AppendLine)
> 			#ftp.storbinary(\'STOR \' +filename, open(filename, \'rb\'))
> 		except AttributeError:
> 			print \"Missing Configuration\", \"To use FTP, add the following to \" + \\
> 			\"your config.ini file\\n\\n\" + \\
> 			\"[FTP]\\n\" + \\
> 			\"Host=ftp.wherever.com\\n\" + \\
> 			\"Username=[yourusername]\\n\" + \\
> 			\"Password=[yourpassword]\"
> 			showerror(\"Missing Configuration\", \"To use FTP, add the following to \" + \\
> 			\"your config.ini file\\n\\n\" + \\
> 			\"[FTP]\\n\" + \\
> 			\"Host=ftp.wherever.com\\n\" + \\
> 			\"Username=[yourusername]\\n\" + \\
> 			\"Password=[yourpassword]\")
> 	t = threading.Thread(target=threadcode, name=\"ftpthread\")
> 	t.setDaemon(1)
> 	t.start()
> 
> 
>   Am I missing something, or is there some specific approach required to do threading?  In all other ways, the
theading works exactly as expected.  That is, when I provide the host, username and password, I get my
button and UI fully functional with no delays, and then the text field gets populated.  
> 
> Thanks,
> _____________________
> Vania Smrkovski
> www.pandorasdream.com

Vania,

You should not make any calls to update Tkinter widgets from another
thread, as you've seen for some platforms it seems to work... some of 
the time but not all (or all platforms!)  I think there are some really 
good examples of threading and Tkinter in the Python Cookbook

A quick google for "python Tkinter threading"  throws this out:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965

I'm sure there are more

Cheers,
Martin.
Martin Franklin | 6 May 11:58 2005
Picon

Internationalising Tkinter


Hi all,

As the subject says my boss in his infinite wisdom has asked me to
investigate "Internationalizing Tkinter"  By this I mean I have a Python
& Tkinter application that among other things produces reports (using
the Canvas postscript method)  What I would very much like to do is have
this application display widgets in different languages (just Spanish
for now but I would like to build / use a frame work where any language
python / Tk supports may be used)

I have googled for "Internationalizing python" and came up with this
excellent article (still reading through it as I write this) but it is
quite old and I wonder if it's a little out of date.

http://python.fyxm.net/workshops/1997-10/proceedings/loewis.html

Any hints, docs, links etc would be much appreciated.

Cheers,
Martin.
Martin Franklin | 6 May 13:11 2005
Picon

Re: Internationalising Tkinter

Martin Franklin wrote:
> Hi all,
> 
> As the subject says my boss in his infinite wisdom has asked me to
> investigate "Internationalizing Tkinter"  By this I mean I have a Python
> & Tkinter application that among other things produces reports (using
> the Canvas postscript method)  What I would very much like to do is have
> this application display widgets in different languages (just Spanish
> for now but I would like to build / use a frame work where any language
> python / Tk supports may be used)
> 
> I have googled for "Internationalizing python" and came up with this
> excellent article (still reading through it as I write this) but it is
> quite old and I wonder if it's a little out of date.
> 
> http://python.fyxm.net/workshops/1997-10/proceedings/loewis.html
> 
> Any hints, docs, links etc would be much appreciated.
> 
> Cheers,
> Martin.

In reply to myself... look in the standard library!  I assume the
gettext module does what I want (sounds like from reading it's
documentation!)

Martin
Fredrik Lundh | 6 May 14:41 2005

Re: Internationalising Tkinter

Martin Franklin wrote:

> As the subject says my boss in his infinite wisdom has asked me to
> investigate "Internationalizing Tkinter"  By this I mean I have a Python
> & Tkinter application that among other things produces reports (using
> the Canvas postscript method)  What I would very much like to do is have
> this application display widgets in different languages (just Spanish
> for now but I would like to build / use a frame work where any language
> python / Tk supports may be used)
>
> I have googled for "Internationalizing python" and came up with this
> excellent article (still reading through it as I write this) but it is
> quite old and I wonder if it's a little out of date.
>
> http://python.fyxm.net/workshops/1997-10/proceedings/loewis.html
>
> Any hints, docs, links etc would be much appreciated.

minimalistic solution:

    from translate import T

    Label(w, text=T("my label")).pack()
    b = Button(w, text=T("ok"))
    etc

where translate.py is

    class Translator:
        def __init__(self, language):
            self.texts = {}
            self.language = language
            ... read translation file for given language and add relevant
                entries to texts ...
        def __getitem__(self, text):
            return self.texts.get(text, text)

    ... get country/language code (e.g. via the locale module)
    T = Translator(language)

where the translation file can be a tab-separated text file (or CSV or any
other format that is easy to maintain in e.g. Excel), a prefix-encoded text
file:

    ** Weight
    DE Gewicht
    DK Vægt
    FI Paino
    GB Weight
    NO Vekt
    SE Vikt
    PL Ciezar
    US Weight
    ES Peso
    MX Peso
    FR Poids
    CZ Vyska

    ** Height
    ...

or some other easy-to-edit-easy-to-parse format.

for maintenance, you can add a hook to the __getitem__ method that
outputs all untranslated items to a file:

    class DebugTranslator(Translator):
        def __getitem__(self, text):
            t = self.texts.get(text)
            if t is None:
                print self.language, text
            return t or text

if you want a more complex way to do the same thing, look for "gettext"
in the library reference.

</F> 

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss <at> python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
Martin Franklin | 6 May 15:41 2005
Picon

Re: Internationalising Tkinter

Fredrik Lundh wrote:
> Martin Franklin wrote:
> 
> 
>>As the subject says my boss in his infinite wisdom has asked me to
>>investigate "Internationalizing Tkinter"  By this I mean I have a Python
>>& Tkinter application that among other things produces reports (using
>>the Canvas postscript method)  What I would very much like to do is have
>>this application display widgets in different languages (just Spanish
>>for now but I would like to build / use a frame work where any language
>>python / Tk supports may be used)
>>
>>I have googled for "Internationalizing python" and came up with this
>>excellent article (still reading through it as I write this) but it is
>>quite old and I wonder if it's a little out of date.
>>
>>http://python.fyxm.net/workshops/1997-10/proceedings/loewis.html
>>
>>Any hints, docs, links etc would be much appreciated.
> 
> 
> minimalistic solution:
> 
>     from translate import T
> 
>     Label(w, text=T("my label")).pack()
>     b = Button(w, text=T("ok"))
>     etc
> 
> where translate.py is
> 
>     class Translator:
>         def __init__(self, language):
>             self.texts = {}
>             self.language = language
>             ... read translation file for given language and add relevant
>                 entries to texts ...
>         def __getitem__(self, text):
>             return self.texts.get(text, text)
> 
>     ... get country/language code (e.g. via the locale module)
>     T = Translator(language)
> 
> where the translation file can be a tab-separated text file (or CSV or any
> other format that is easy to maintain in e.g. Excel), a prefix-encoded text
> file:
> 
>     ** Weight
>     DE Gewicht
>     DK Vægt
>     FI Paino
>     GB Weight
>     NO Vekt
>     SE Vikt
>     PL Ciezar
>     US Weight
>     ES Peso
>     MX Peso
>     FR Poids
>     CZ Vyska
> 
>     ** Height
>     ...
> 
> or some other easy-to-edit-easy-to-parse format.
> 
> for maintenance, you can add a hook to the __getitem__ method that
> outputs all untranslated items to a file:
> 
>     class DebugTranslator(Translator):
>         def __getitem__(self, text):
>             t = self.texts.get(text)
>             if t is None:
>                 print self.language, text
>             return t or text
> 
> if you want a more complex way to do the same thing, look for "gettext"
> in the library reference.
> 
> </F> 

Thanks Fredrik,

It's an obvious solution now I've seen it :-)

Although I think you mean __call__ since you are calling the instance
T() not slicing or indexing it T[]

I've started playing with the gettext module and I must say I quite like
the automatic features of the system - the generation of the pot files
from the source code, and the compiling of the mo files.

Whichever method I choose, I'm glad to say with python it's _almost_ too 
easy!

Martin.

Gmane