From woswwf at yahoo.co.uk Mon May 2 18:41:50 2005 From: woswwf at yahoo.co.uk (Brill IanT) Date: Mon, 2 May 2005 17:41:50 +0100 (BST) Subject: [Tkinter-discuss] Problem looping Message-ID: <20050502164151.17812.qmail@web26606.mail.ukl.yahoo.com> 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20050502/77610d5d/attachment.htm From Cameron at Phaseit.net Mon May 2 18:44:54 2005 From: Cameron at Phaseit.net (Cameron Laird) Date: Mon, 2 May 2005 16:44:54 +0000 Subject: [Tkinter-discuss] Problem looping In-Reply-To: <20050502164151.17812.qmail@web26606.mail.ukl.yahoo.com> References: <20050502164151.17812.qmail@web26606.mail.ukl.yahoo.com> Message-ID: <20050502164454.GA15435@lairds.us> 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: . Do you want help translating this into Tkinter? From harlinseritt at yahoo.com Tue May 3 12:38:23 2005 From: harlinseritt at yahoo.com (Harlin Seritt) Date: Tue, 3 May 2005 03:38:23 -0700 (PDT) Subject: [Tkinter-discuss] Tkinter-discuss Digest, Vol 15, Issue 1 In-Reply-To: Message-ID: <20050503103824.11075.qmail@web52208.mail.yahoo.com> 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() 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) 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.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 -------------- 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 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: . 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-discuss Digest, Vol 15, Issue 1 ********************************************** __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20050503/388e609f/attachment.html From Cameron at Phaseit.net Tue May 3 17:40:15 2005 From: Cameron at Phaseit.net (Cameron Laird) Date: Tue, 3 May 2005 15:40:15 +0000 Subject: [Tkinter-discuss] Tkinter-discuss Digest, Vol 15, Issue 1 In-Reply-To: <20050503103824.11075.qmail@web52208.mail.yahoo.com> References: <20050503103824.11075.qmail@web52208.mail.yahoo.com> Message-ID: <20050503154015.GA7455@lairds.us> 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. From vania at pandorasdream.com Wed May 4 17:20:45 2005 From: vania at pandorasdream.com (Vania Smrkovski) Date: 4 May 2005 15:20:45 -0000 Subject: [Tkinter-discuss] Tkinter and threading Message-ID: 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 From mfranklin1 at gatwick.westerngeco.slb.com Thu May 5 08:35:04 2005 From: mfranklin1 at gatwick.westerngeco.slb.com (Martin Franklin) Date: Thu, 05 May 2005 07:35:04 +0100 Subject: [Tkinter-discuss] Tkinter and threading In-Reply-To: References: Message-ID: 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. From mfranklin1 at gatwick.westerngeco.slb.com Fri May 6 11:58:22 2005 From: mfranklin1 at gatwick.westerngeco.slb.com (Martin Franklin) Date: Fri, 06 May 2005 10:58:22 +0100 Subject: [Tkinter-discuss] Internationalising Tkinter Message-ID: 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. From mfranklin1 at gatwick.westerngeco.slb.com Fri May 6 13:11:57 2005 From: mfranklin1 at gatwick.westerngeco.slb.com (Martin Franklin) Date: Fri, 06 May 2005 12:11:57 +0100 Subject: [Tkinter-discuss] Internationalising Tkinter In-Reply-To: References: Message-ID: 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 From fredrik at pythonware.com Fri May 6 14:41:24 2005 From: fredrik at pythonware.com (Fredrik Lundh) Date: Fri, 6 May 2005 14:41:24 +0200 Subject: [Tkinter-discuss] Internationalising Tkinter References: Message-ID: 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. From mfranklin1 at gatwick.westerngeco.slb.com Fri May 6 15:41:45 2005 From: mfranklin1 at gatwick.westerngeco.slb.com (Martin Franklin) Date: Fri, 06 May 2005 14:41:45 +0100 Subject: [Tkinter-discuss] Internationalising Tkinter In-Reply-To: References: Message-ID: 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. > > 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. From fredrik at pythonware.com Fri May 6 18:45:08 2005 From: fredrik at pythonware.com (Fredrik Lundh) Date: Fri, 6 May 2005 18:45:08 +0200 Subject: [Tkinter-discuss] Internationalising Tkinter References: Message-ID: Martin Franklin wrote: > Although I think you mean __call__ since you are calling the instance > T() not slicing or indexing it T[] I think I really meant def get(self, text): return self.texts.get(text, text) ... get country/language code (e.g. via the locale module) T = Translator(language).get > Whichever method I choose, I'm glad to say with python it's _almost_ too > easy! that's the whole point of using Python, of course. From rowen at cesmail.net Mon May 9 21:04:34 2005 From: rowen at cesmail.net (Russell E. Owen) Date: Mon, 09 May 2005 12:04:34 -0700 Subject: [Tkinter-discuss] Tkinter and threading References: Message-ID: In article , 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.... You cannot safely make Tkinter from any thread other than the one that started the tkinter mainloop (i.e. your main thread unless you are doing something really weird). As far as network communication goes, you have several options... - Use the free "twisted framework". It integrates with Tkinter and other GUI toolkits, handles most of the standard protocols and is reported to be very good. - For ftp, do the transfer in the background and poll the state. This is especially appropriate if you have multiple transfers since one polling loop can poll all of them. For an example see RO.Comm.FTPGet and the associated widget RO.Wdg.FTPLogWdg in the RO package . - For TCP/IP use a tcl socket and use file events to handle incoming data and errors. This has the advantage of being asynchronous (and is completely cross platform unlike the a similar technique at the Tkinter level). For an example see RO.Comm.TCPConnection and the underlying RO.Comm.TkSocket. -- Russell P.S. there were rumors a few years ago that you could safely generate Tkinter events from a background thread and have the main thread handle them. However, I am pretty sure (based on my own tests) that it's not actually safe to do that. My suggestion is to NEVER deal with Tkinter or Tk from a background thread. From harlinseritt at yahoo.com Tue May 10 12:25:41 2005 From: harlinseritt at yahoo.com (Harlin Seritt) Date: Tue, 10 May 2005 03:25:41 -0700 (PDT) Subject: [Tkinter-discuss] Tkinter-discuss Digest, Vol 15, Issue 5 In-Reply-To: 6667 Message-ID: <20050510102542.90694.qmail@web52207.mail.yahoo.com> I think this sort of thing is asked more in this list than anywhere else :-) Try using Tk().after(milliseconds, function/method). Sure it can eat up some memory but it works pretty well. 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. Re: Tkinter and threading (Russell E. Owen) ---------------------------------------------------------------------- Message: 1 Date: Mon, 09 May 2005 12:04:34 -0700 From: "Russell E. Owen" Subject: Re: [Tkinter-discuss] Tkinter and threading To: tkinter-discuss at python.org Message-ID: In article , 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.... You cannot safely make Tkinter from any thread other than the one that started the tkinter mainloop (i.e. your main thread unless you are doing something really weird). As far as network communication goes, you have several options... - Use the free "twisted framework". It integrates with Tkinter and other GUI toolkits, handles most of the standard protocols and is reported to be very good. - For ftp, do the transfer in the background and poll the state. This is especially appropriate if you have multiple transfers since one polling loop can poll all of them. For an example see RO.Comm.FTPGet and the associated widget RO.Wdg.FTPLogWdg in the RO package . - For TCP/IP use a tcl socket and use file events to handle incoming data and errors. This has the advantage of being asynchronous (and is completely cross platform unlike the a similar technique at the Tkinter level). For an example see RO.Comm.TCPConnection and the underlying RO.Comm.TkSocket. -- Russell P.S. there were rumors a few years ago that you could safely generate Tkinter events from a background thread and have the main thread handle them. However, I am pretty sure (based on my own tests) that it's not actually safe to do that. My suggestion is to NEVER deal with Tkinter or Tk from a background thread. ------------------------------ _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss at python.org http://mail.python.org/mailman/listinfo/tkinter-discuss End of Tkinter-discuss Digest, Vol 15, Issue 5 ********************************************** --------------------------------- Yahoo! Mail Mobile Take Yahoo! Mail with you! Check email on your mobile phone. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20050510/4e9ed0ad/attachment.htm From vania at pandorasdream.com Tue May 10 14:10:48 2005 From: vania at pandorasdream.com (Vania Smrkovski) Date: 10 May 2005 12:10:48 -0000 Subject: [Tkinter-discuss] Tkinter-discuss Digest, Vol 15, Issue 5 Message-ID: Thanks, Harlin, As it happens, I stumbled upon that solution somewhere else, too, and it worked. The Tkinter sites are more than honest in not having the level of documentation that would avoid my repeat question. But having been involved in open source for some time, I have to say I am very thankful that no one has been unduly rude to me for asking a FAQ. I appreciate your answer, too. Now, I am working on integrating my wireless palm handheld with Python services. I am surprised that there still are not palm interpreters for PalmOS (beyond Pippy, I mean), but I can stand to learn a little C again after all these years of scripting.... Thanks again, _____________________ Vania Smrkovski www.pandorasdream.com ----- Original Message ----- I think this sort of thing is asked more in this list than anywhere else :-) Try using Tk().after(milliseconds, function/method). Sure it can eat up some memory but it works pretty well. 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. Re: Tkinter and threading (Russell E. Owen) ---------------------------------------------------------------------- Message: 1 Date: Mon, 09 May 2005 12:04:34 -0700 From: \"Russell E. Owen\" Subject: Re: [Tkinter-discuss] Tkinter and threading To: tkinter-discuss at python.org Message-ID: In article , 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.... You cannot safely make Tkinter from any thread other than the one that started the tkinter mainloop (i.e. your main thread unless you are doing something really weird). As far as network communication goes, you have several options... - Use the free \"twisted framework\". It integrates with Tkinter and other GUI toolkits, handles most of the standard protocols and is reported to be very good. - For ftp, do the transfer in the background and poll the state. This is especially appropriate if you have multiple transfers since one polling loop can poll all of them. For an example see RO.Comm.FTPGet and the associated widget RO.Wdg.FTPLogWdg in the RO package . - For TCP/IP use a tcl socket and use file events to handle incoming data and errors. This has the advantage of being asynchronous (and is completely cross platform unlike the a similar technique at the Tkinter level). For an example see RO.Comm.TCPConnection and the underlying RO.Comm.TkSocket. -- Russell P.S. there were rumors a few years ago that you could safely generate Tkinter events from a background thread and have the main thread handle them. However, I am pretty sure (based on my own tests) that it\'s not actually safe to do that. My suggestion is to NEVER deal with Tkinter or Tk from a background thread. ------------------------------ _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss at python.org http://mail.python.org/mailman/listinfo/tkinter-discuss End of Tkinter-discuss Digest, Vol 15, Issue 5 ********************************************** --------------------------------- Yahoo! Mail Mobile Take Yahoo! Mail with you! Check email on your mobile phone. From mathieu.cousy at cena.fr Wed May 11 15:50:09 2005 From: mathieu.cousy at cena.fr (Mathieu Cousy) Date: 11 May 2005 15:50:09 +0200 Subject: [Tkinter-discuss] Python 2.1 with tcl/tk 8.4.5 Message-ID: <1115819409.32571.21.camel@vieno> Hi all, For different reasons I need to build a python interpreter 2.1 linked with tk 8.4.5 library (I am working on a Debian Woody system). The whole python building process succeeded but when I try to run the tkinter demo (Demo/tkinter/guido/AttrDialog.py) an exception is raised on any events (mouse pressed, key pressed...). The exception is : Exception in Tkinter callback Traceback (most recent call last): File "/home/cousy/dev/debian/Python-2.1.3/install/lib/python2.1/lib-tk/Tkinter.py", line 1284, in __call__ args = apply(self.subst, args) File "/home/cousy/dev/debian/Python-2.1.3/install/lib/python2.1/lib-tk/Tkinter.py", line 1024, in _substitute e.num = getint(b) ValueError: invalid literal for int(): ?? Is there any kind of incompatibility between these versions ? Any hints, docs, links etc would be much appreciated. Thanks in adavance, Mathieu. -- Mathieu COUSY Prestataire QUATERNOVE @ Centre d'Etudes de la Navigation Aerienne (CENA) Division I.C.S. Batiment 1608, Route peripherique Est 91200 Athis-Mons, FRANCE tel : 01 69 57 68 35 Mail : cousy at ath.cena.fr -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20050511/5bce2be6/attachment.html From jepler at unpythonic.net Wed May 11 20:24:22 2005 From: jepler at unpythonic.net (Jeff Epler) Date: Wed, 11 May 2005 13:24:22 -0500 Subject: [Tkinter-discuss] Python 2.1 with tcl/tk 8.4.5 In-Reply-To: <1115819409.32571.21.camel@vieno> References: <1115819409.32571.21.camel@vieno> Message-ID: <20050511182422.GB12225@unpythonic.net> short answer: yes, it's an incompatibility. You may be able to modify Tkinter.py with a patch that is on sourceforge, though. http://python.org/sf/698517 http://python.org/sf/707701 Jeff -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.python.org/pipermail/tkinter-discuss/attachments/20050511/df77e7ba/attachment.pgp From fredrik at pythonware.com Wed May 11 20:59:18 2005 From: fredrik at pythonware.com (Fredrik Lundh) Date: Wed, 11 May 2005 20:59:18 +0200 Subject: [Tkinter-discuss] Python 2.1 with tcl/tk 8.4.5 References: <1115819409.32571.21.camel@vieno> Message-ID: Mathieu Cousy wrote: > For different reasons I need to build a python interpreter 2.1 linked > with tk 8.4.5 library (I am working on a Debian Woody system). The whole > python building process succeeded but when I try to run the tkinter demo > (Demo/tkinter/guido/AttrDialog.py) an exception is raised on any events > (mouse pressed, key pressed...). The exception is : > > ValueError: invalid literal for int(): ?? > > Is there any kind of incompatibility between these versions ? > Any hints, docs, links etc would be much appreciated. did you try googling for the error message? http://mail.python.org/pipermail/python-list/2003-July/171497.html (in recent versions of Tk, the authors changed a field that was defined to hold "an undefined integer value" from using an un- defined integer value to "??". The best solution for your case is to hack Tkinter.py; look for the "_substitute" method and tweak it to match the newer versions). From mathieu.cousy at cena.fr Thu May 12 09:38:22 2005 From: mathieu.cousy at cena.fr (Mathieu Cousy) Date: 12 May 2005 09:38:22 +0200 Subject: [Tkinter-discuss] Python 2.1 with tcl/tk 8.4.5 In-Reply-To: <20050511182422.GB12225@unpythonic.net> References: <1115819409.32571.21.camel@vieno> <20050511182422.GB12225@unpythonic.net> Message-ID: <1115883502.13225.0.camel@vieno> Thanks ! Mathieu. On Wed, 2005-05-11 at 20:24, Jeff Epler wrote: short answer: yes, it's an incompatibility. You may be able to modify Tkinter.py with a patch that is on sourceforge, though. http://python.org/sf/698517 http://python.org/sf/707701 Jeff -- Mathieu COUSY Prestataire QUATERNOVE @ Centre d'Etudes de la Navigation Aerienne (CENA) Division I.C.S. Batiment 1608, Route peripherique Est 91200 Athis-Mons, FRANCE tel : 01 69 57 68 35 Mail : cousy at ath.cena.fr -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20050512/b8afc6f2/attachment.html From mathieu.cousy at cena.fr Wed May 11 15:39:18 2005 From: mathieu.cousy at cena.fr (Mathieu Cousy) Date: 11 May 2005 15:39:18 +0200 Subject: [Tkinter-discuss] Python 2.1 with tcl/tk 8.4.5 Message-ID: <1115818759.13424.19.camel@vieno> Hi all, For different reasons I need to build a python interpreter 2.1 linked with tk 8.4.5 library (I am working on a Debian Woody system). The whole python building process succeeded but when I try to run the tkinter demo (Demo/tkinter/guido/AttrDialog.py) an exception is raised on any events (mouse pressed, key pressed...). The exception is : Exception in Tkinter callback Traceback (most recent call last): File "/home/cousy/dev/debian/Python-2.1.3/install/lib/python2.1/lib-tk/Tkinter.py", line 1284, in __call__ args = apply(self.subst, args) File "/home/cousy/dev/debian/Python-2.1.3/install/lib/python2.1/lib-tk/Tkinter.py", line 1024, in _substitute e.num = getint(b) ValueError: invalid literal for int(): ?? Is there any kind of incompatibility between these versions ? Any hints, docs, links etc would be much appreciated. Thanks in adavance, Mathieu. -- Mathieu COUSY Prestataire QUATERNOVE @ Centre d'Etudes de la Navigation Aerienne (CENA) Division I.C.S. Batiment 1608, Route peripherique Est 91200 Athis-Mons, FRANCE tel : 01 69 57 68 35 Mail : cousy at ath.cena.fr -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20050511/56e2dc3b/attachment.htm From dking at nrao.edu Fri May 13 17:15:44 2005 From: dking at nrao.edu (David King) Date: Fri, 13 May 2005 09:15:44 -0600 Subject: [Tkinter-discuss] Hooking into custom Tcl/Tk Widgets from Tkinter (vis) In-Reply-To: <20050429182522.GC6636@unpythonic.net> References: <426FBF46.7040708@nrao.edu> <4272685B.6090606@nrao.edu> <20050429182522.GC6636@unpythonic.net> Message-ID: <4284C4A0.4030705@nrao.edu> Thank you, Jeff, for your suggested code to send Tkinter's interpreter pointer into C++; along with Mr. Lundh's advice, you are helping me to piece together what I need to make my custom C++/Tk widget available in Tkinter. I still would like to hear from someone on how I'd invoke creation of my new custom widget and send it commands from python. The C++ code registers callbacks for both these sorts of things with the Tcl interpreter, so that they become available as _Tcl/Tk_ commands. But how can such commands be sent from python to Tkinter's Tcl/Tk interpreter? ------ At the risk of putting too many questions in one email, I'll also mention the other main problem I'm wrestling with. I need to understand the 'interactive' aspects of Tkinter--i.e., how _both_ the Tkinter gui(s) and python's sommand-line interpreter can remain 'live' at the same time, each responding to its own type of input. I need my user to ba able to send commands to my widget via python commands as well as via gui input. I know that matplotlib (esp. on top of Tkinter, and operated from the IPython shell) manages to do this, and the matplotlib folks seem to think Tkinter even has advantages over other widgetsets such as GTK for interactive use. In the most elementary Tkinter examples I run, however, I must give up all further python input when I enter tk.mainloop(). IPython/matplotlib give hints that perhaps they manage the trick by creating a second thread for the Tkinter loop to run in. That may be an ok solution, if I can truly understand where/how the one-thread-at-a-time-only locks need to be used (apparently both the python and Tcl interpreters each need such exclusivity; my widget library code will certainly need it as well). Alternatively,... is there perhaps another way of merging the console and gui-event input streams, _without_ using more than one process/thread? All suggestions welcome. (Is there a 'bible' that lays all this out? I'm willing to spring for the book if necessary). David King Jeff Epler wrote: > On Fri, Apr 29, 2005 at 11:01:15AM -0600, David King wrote: > >>But let's leave WCK aside and just talk about plain Tkinter for the moment. >> I'd like to understand how my custom C++/Tk widget could make itself known >>in the usual Tkinter world. On the C++ side, essentially all my Tcl/Tk API >>calls interact with a tcl interpreter handle ('Tcl_Interp*'). Presumably I >>need to get the one Tkinter uses, instead of creating an interpreter myself >>(and also to let Tkinter handle the event loop, rather than doing _that_ >>myself). I'm wondering how I get this handle from Tkinter. > > > Here is some "C" code that I have used for this purpose. > > // this code is in the public domain > static Tcl_Interp *get_interpreter(PyObject *tkapp) { > long interpaddr; > PyObject *interpaddrobj = PyObject_CallMethod(tkapp, "interpaddr", NULL); > if(interpaddrobj == NULL) { return NULL; } > interpaddr = PyInt_AsLong(interpaddrobj); > Py_DECREF(interpaddrobj); > if(interpaddr == -1) { return NULL; } > return (Tcl_Interp*)interpaddr; > } > > 'PyObject *tkapp' is the thing returned by _tkinter.create(), and available as > the 'tk' attribute on all Tkinter widgets. > > This is not foolproof, because passing in an object like > class K: > interpaddr = 0 > will soon lead to a crash. But if you can trust the calling code to only pass in > "real" interpreters, it should work just fine. > > Jeff From jepler at unpythonic.net Fri May 13 18:40:52 2005 From: jepler at unpythonic.net (Jeff Epler) Date: Fri, 13 May 2005 11:40:52 -0500 Subject: [Tkinter-discuss] Hooking into custom Tcl/Tk Widgets from Tkinter (vis) In-Reply-To: <4284C4A0.4030705@nrao.edu> References: <426FBF46.7040708@nrao.edu> <4272685B.6090606@nrao.edu> <20050429182522.GC6636@unpythonic.net> <4284C4A0.4030705@nrao.edu> Message-ID: <20050513164052.GA31208@unpythonic.net> [copied to tkinter-discuss for the benefit of others] On Fri, May 13, 2005 at 09:15:44AM -0600, David King wrote: > I still would like to hear from someone on how I'd invoke creation of my > new custom widget and send it commands from python. The C++ code registers > callbacks for both these sorts of things with the Tcl interpreter, so that > they become available as _Tcl/Tk_ commands. But how can such commands be > sent from python to Tkinter's Tcl/Tk interpreter? By using the "call" method of the "tkapp" object, which is the .tk attribute of each widget. Did you look at how widgets methods are implemented? def activate(self, index): """Activate item identified by INDEX.""" self.tk.call(self._w, 'activate', index) This is turned into a tcl command like .135964900 activate 1 You can call any Tk command you like in this way. > At the risk of putting too many questions in one email, I'll also mention > the other main problem I'm wrestling with. I need to understand the > 'interactive' aspects of Tkinter--i.e., how _both_ the Tkinter gui(s) and > python's sommand-line interpreter can remain 'live' at the same time, each > responding to its own type of input. I don't know exactly how all this works. I don't write threaded code, so I don't know much about the pitfalls of it on various systems. On Unix, you have _tkinter.createfilehandler() so that you can turn your non-gui I/O into something that works via callbacks. Or you can arrange for "update" to be called periodically from a non-Tk event loop, never calling the Tk mainloop function. Finally, you could do something operating-system specific, such as (on Unix) finding the file numbers for both the X server connection and stdin, then feeding them both to select. Finally, if you want to use the threading approach, I think it's safe if you use the GUI only in the main thread---including things like getting and setting the values of Tkinter.Variables---and do anything else you like in the other threads. But as I said, I don't generally write threaded programs. Jeff -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.python.org/pipermail/tkinter-discuss/attachments/20050513/5433d797/attachment.pgp From fredrik at pythonware.com Fri May 13 19:21:05 2005 From: fredrik at pythonware.com (Fredrik Lundh) Date: Fri, 13 May 2005 19:21:05 +0200 Subject: [Tkinter-discuss] Hooking into custom Tcl/Tk Widgets fromTkinter (vis) References: <426FBF46.7040708@nrao.edu> <4272685B.6090606@nrao.edu> <20050429182522.GC6636@unpythonic.net> <4284C4A0.4030705@nrao.edu> Message-ID: "David King" skrev i meddelandet news:4284C4A0.4030705 at nrao.edu... > Thank you, Jeff, for your suggested code to send Tkinter's interpreter > pointer into C++; along with Mr. Lundh's advice, you are helping me to piece > together what I need to make my custom C++/Tk widget available in Tkinter. > > I still would like to hear from someone on how I'd invoke creation of my new > custom widget and send it commands from python. The C++ code registers > callbacks for both these sorts of things with the Tcl interpreter, so that > they become available as _Tcl/Tk_ commands. But how can such commands be > sent from python to Tkinter's Tcl/Tk interpreter? > > ------ > > > At the risk of putting too many questions in one email, I'll also mention > the other main problem I'm wrestling with. I need to understand the > 'interactive' aspects of Tkinter--i.e., how _both_ the Tkinter gui(s) and > python's sommand-line interpreter can remain 'live' at the same time, each > responding to its own type of input. I need my user to ba able to send > commands to my widget via python commands as well as via gui input. I know > that matplotlib (esp. on top of Tkinter, and operated from the IPython > shell) manages to do this, and the matplotlib folks seem to think Tkinter > even has advantages over other widgetsets such as GTK for interactive use. > In the most elementary Tkinter examples I run, however, I must give up all > further python input when I enter tk.mainloop(). > > IPython/matplotlib give hints that perhaps they manage the trick by creating > a second thread for the Tkinter loop to run in. That may be an ok solution, > if I can truly understand where/how the one-thread-at-a-time-only locks need > to be used (apparently both the python and Tcl interpreters each need such > exclusivity; my widget library code will certainly need it as well). > > Alternatively,... is there perhaps another way of merging the console and > gui-event input streams, _without_ using more than one process/thread? > > All suggestions welcome. (Is there a 'bible' that lays all this out? I'm > willing to spring for the book if necessary). > > David King > > > > > Jeff Epler wrote: > > On Fri, Apr 29, 2005 at 11:01:15AM -0600, David King wrote: > > > >>But let's leave WCK aside and just talk about plain Tkinter for the moment. > >> I'd like to understand how my custom C++/Tk widget could make itself known > >>in the usual Tkinter world. On the C++ side, essentially all my Tcl/Tk API > >>calls interact with a tcl interpreter handle ('Tcl_Interp*'). Presumably I > >>need to get the one Tkinter uses, instead of creating an interpreter myself > >>(and also to let Tkinter handle the event loop, rather than doing _that_ > >>myself). I'm wondering how I get this handle from Tkinter. > > > > > > Here is some "C" code that I have used for this purpose. > > > > // this code is in the public domain > > static Tcl_Interp *get_interpreter(PyObject *tkapp) { > > long interpaddr; > > PyObject *interpaddrobj = PyObject_CallMethod(tkapp, "interpaddr", NULL); > > if(interpaddrobj == NULL) { return NULL; } > > interpaddr = PyInt_AsLong(interpaddrobj); > > Py_DECREF(interpaddrobj); > > if(interpaddr == -1) { return NULL; } > > return (Tcl_Interp*)interpaddr; > > } > > > > 'PyObject *tkapp' is the thing returned by _tkinter.create(), and available as > > the 'tk' attribute on all Tkinter widgets. > > > > This is not foolproof, because passing in an object like > > class K: > > interpaddr = 0 > > will soon lead to a crash. But if you can trust the calling code to only pass in > > "real" interpreters, it should work just fine. > > > > Jeff From fredrik at pythonware.com Fri May 13 19:29:05 2005 From: fredrik at pythonware.com (Fredrik Lundh) Date: Fri, 13 May 2005 19:29:05 +0200 Subject: [Tkinter-discuss] Hooking into custom Tcl/Tk Widgets fromTkinter (vis) References: <426FBF46.7040708@nrao.edu> <4272685B.6090606@nrao.edu> <20050429182522.GC6636@unpythonic.net> <4284C4A0.4030705@nrao.edu> Message-ID: David King wrote: > I still would like to hear from someone on how I'd invoke creation of my new > custom widget and send it commands from python. The C++ code registers > callbacks for both these sorts of things with the Tcl interpreter, so that > they become available as _Tcl/Tk_ commands. But how can such commands be > sent from python to Tkinter's Tcl/Tk interpreter? if you have PIL on your machine, you can look at the tkImaging and _imagingtk C modules, and the PIL/ImageTk.py module. the latter contains the following block: tk = self.__photo.tk try: tk.call("PyImagingPhoto", self.__photo, block.id) except Tkinter.TclError, v: # activate Tkinter hook try: import _imagingtk try: _imagingtk.tkinit(tk.interpaddr(), 1) except AttributeError: _imagingtk.tkinit(id(tk), 0) tk.call("PyImagingPhoto", self.__photo, block.id) except (ImportError, AttributeError, Tkinter.TclError): raise # configuration problem; cannot attach to Tkinter in this case, self.__photo happens to be a Tkinter PhotoImage object, which has a "tk" attribute that contains the Tkinter context. you'll find the same attribute on all Tkinter widgets. the tk.call() line calls PIL's custom Tcl command (PyImagingPhoto) with a reference to the photo object as the first argument, and a "block" attribute as the second argument. the error handler, finally, traps errors from Tcl, and attempts to (re)register the command for the current Tcl interpreter. (the interpaddr() function returns a Tcl_Interp pointer cast to a long integer; the second case is there to deal with old Tkinter versions. to see how that case works, see the _imagingtk.c source code). to handle your acse, replace self.__photo with some suitable widget, call your own command with suitable arguments, and replace _imagingtk with your own C extension. (if you need more sample code, grab the PIL sources). From dking at nrao.edu Mon May 23 20:03:01 2005 From: dking at nrao.edu (David King) Date: Mon, 23 May 2005 12:03:01 -0600 Subject: [Tkinter-discuss] gui/thread issues Message-ID: <42921AD5.7050508@nrao.edu> >>>>David King wrote [private note, not a previous thread]: David> Dear Prabhu: I noticed a forum thread discussing gui David> threading, e.g.: David> http://www.scipy.net/pipermail/ipython-dev/2004-November/000299.html David> I'm asking you to have mercy on a fairly new python user. David> I want to use Tkinter and still have the python console David> remain 'live'. I have the impression you have gone far David> more deeply into this than I, and that others as well have David> thought on it. Yet I still have no solid doc on when David> tk.mainloop() is necessary and when not, on how to process David> Tkinter events while the python interpreter remains active David> to input. I have prowled around fairly extensively in a David> (so-far) vain search for the definitive answer to this. Prabhu Ramachandran wrote: > The answer strongly depends on what particular Python interpreter you > are using. Under IPython (without -wthread or -gthread) or vanilla > Python, tk.mainloop() is not necessary and in fact should not be > called if you want the console active. Even if you do not call the > mainloop, events will still be processed and your application will work > just fine. > > However, if you are running apps written with pyGTK or wxPython then > you can't get away with running a Tkinter app and not calling > mainloop. With IPython, there is an option (-wthread/-gthread) that > starts up the GTK and wx mainloops in a secondary thread and allows > for an interactive console on the main thread. In this case, if you > start a Tkinter application, it will in all probability not work. > This is because GTK and wx have their own event handling loops that do > not play nicely with the Tkinter event loop in another thread. To get > around this, IPython also provides a -tk flag that processes tk events > every so often. Now this works best only if tkinter is compiled with > thread support, if not things break badly. > > With the IDLE interpreter things are different since IDLE itself > starts the mainloop and recent versions of IDLE have some issues and > you might want to read this thread: > > http://aspn.activestate.com/ASPN/Mail/Message/edu-sig/1818398 > > > I hope I have answered the question clearly enough and not confused > you any more. Dear Prabhu: Thanks you very much for your note regarding Python-gui interaction; very kind of you. Some of what I've read on the net does add to my confusion, but definitely _not_ your reply; it is helping me piece things together. In the short run, I would be ok just using Tk alone on IPython or plain Python, although I'd be happiest having the whole picture. In the long run, our project will want to serve varied users, who will no doubt want to use python in their own way. Russell Owen also mentioned that Tkinter's 'after[_idle]' might not work without calling mainloop() explicitly; although perhaps I can do without those features, I'd certainly like to know if this is the case, and what else to watch out for of this sort.... I will continue to study what I have (that forum-thread link was helpful too--thanks). But I can't help feeling other python gui programmers must run up against this problem too, and wonder why practically all the standard Tkinter doc (including Grayson's book) just says "you gotta call mainloop() to make anything actually appear and respond in Tkinter" (manifestly untrue). I'm beginning to suspect that although the information I seek resides in various people's heads, noone has written a real overview of gui event loop/threading issues, with simple pictures of what is going on, the various cases known to work or not work, and guidance on how to write the most generally usable gui apps in python while keeping the interpreter 'alive'. Ideally, I'd like to see an overview considering all the cases: - Tkinter, wx, GTK - plain python, IPython, IDLE - IPython -wthread -gthread and -tk flags - compiling Tkinter with/without thread support Perhaps when I'm done with my researches I'll write such an article myself... :-). Meanwhile, any other pointers or good clues you can give would certainly be welcome. There are also some intriguing comments in _tkinter.c on threading; my 'ideal guithreads article' would also provide an overview of that code, as an aid in reading it. (I will be dwelling on thc C/C++ side as well in my project. FYI, I am trying to port a C++ library that displays our in-house types of (astronomical) data, so that it can operate under python control. As currently used, it is already a custom Tk widget (outside of Tkinter/python), and has a command interface as well (via a home-grown inter-process communication framework), so that it is controlled through both mouse and scripting. I want to replace the current command interface with python scripting). As a concrete question, may I ask you how to tell if 'tkinter is compiled with thread support'? (I note that a 'test_threading' passed in the 'make test' log for my python; I presume that if threading was on for my python build, it was on for building _tkinter.so...(?)) Anyway, thanks again for your help; I hope I haven't taken too much of your time. Regards, David King From perry at stsci.edu Mon May 23 20:25:04 2005 From: perry at stsci.edu (Perry Greenfield) Date: Mon, 23 May 2005 14:25:04 -0400 Subject: [Tkinter-discuss] [AstroPy] Re: gui/thread issues In-Reply-To: <42921AD5.7050508@nrao.edu> References: <42921AD5.7050508@nrao.edu> Message-ID: <729cb13ce6781f439540fdcdd72e6a7b@stsci.edu> On May 23, 2005, at 2:03 PM, David King wrote: > > In the short run, I would be ok just using Tk alone on IPython or plain > Python, although I'd be happiest having the whole picture. In the > long run, > our project will want to serve varied users, who will no doubt want to > use > python in their own way. > Note that Tkinter windows under the standard Python interpreter are only 'alive' when the intepreter prompt is active. If you invoke a function that takes a long time, the window will be unresponsive since the Tk events are only handled within the Python interpreter loop. In this respect IPython should have an advantage with threading. > > I will continue to study what I have (that forum-thread link was > helpful > too--thanks). But I can't help feeling other python gui programmers > must > run up against this problem too, and wonder why practically all the > standard > Tkinter doc (including Grayson's book) just says "you gotta call > mainloop() > to make anything actually appear and respond in Tkinter" (manifestly > untrue). I think the problem is that, by and large, almost all uses of GUIs are centered on their being standalone applications and thus little thought is given to having a command line coexisting with the GUI. Our needs are pretty atypical in this regard. Perry Greenfield From fredrik at pythonware.com Wed May 25 09:13:08 2005 From: fredrik at pythonware.com (Fredrik Lundh) Date: Wed, 25 May 2005 09:13:08 +0200 Subject: [Tkinter-discuss] gui/thread issues References: <42921AD5.7050508@nrao.edu> Message-ID: David King wrote: > I will continue to study what I have (that forum-thread link was helpful > too--thanks). But I can't help feeling other python gui programmers must > run up against this problem too, and wonder why practically all the standard > Tkinter doc (including Grayson's book) just says "you gotta call mainloop() > to make anything actually appear and respond in Tkinter" (manifestly untrue). no, that's 100% true if you use Tkinter from a running Python program. that's how most people use Python, and that's how most Tkinter programs are meant to be used. if you type Python commands into an interactive prompt, the exact behaviour depends on the program that offers the interactive environment. if that pro- gram already runs a main loop (or provides some other kind of message pump), *and* the program runs your commands/snippets *in its own process*, Tkinter commands *may* work without additional message handling. Tkinter itself has no control over that. From mfranklin1 at gatwick.westerngeco.slb.com Thu May 26 11:22:18 2005 From: mfranklin1 at gatwick.westerngeco.slb.com (Martin Franklin) Date: Thu, 26 May 2005 10:22:18 +0100 Subject: [Tkinter-discuss] tk 8.3 and unicode Message-ID: Hi all, Another possibly silly question..... My prodcution machines are stuck at python2.2 and tk 8.3 and *I* have a problem with the following code:- import codecs import Tkinter root=Tkinter.Tk() var = Tkinter.StringVar() e = Tkinter.Entry(root, textvariable=var) e.pack() var.set(u"\xa3 \xa3") #~ f = codecs.open("ce.tst", "w", "utf-8") #~ f.write(var.get()) #~ f.close() print repr(var.get()) root.mainloop() The GUI shows up and is correct, however when I un-comment the three save to file lines it fails with: UnicodeError: ASCII decoding error: ordinal not in range(128) Indeed when I look at the printed output from the code is shows:- '\xc2\xa3 \xc2\xa3' rather than what I put into that variable..... I assume this is a feature of tk 8.3 - i've tested on py2.3+tk8.4 and py2.4+tk8.5 and both those version *just work* is there any get around? Cheers Martin From jepler at unpythonic.net Fri May 27 16:06:20 2005 From: jepler at unpythonic.net (Jeff Epler) Date: Fri, 27 May 2005 09:06:20 -0500 Subject: [Tkinter-discuss] tk 8.3 and unicode In-Reply-To: References: Message-ID: <20050527140617.GA8187@unpythonic.net> This bug must have been fixed between Python 2.2 + Tk 8.3 and Python 2.3 + Tk 8.4. Running a slightly different test program, I get this output on Fedora Core 1 (Python 2.2 + Tk 8.3): '\xc2\xa3' and this output on Fedora Core 2 (Python 2.3 + Tk 8.4): u'\xa3' You could try defining a class UStringVar(Tkinter.StringVar): def get(self): return Tkinter.StringVar.get(self).decode("utf-8") and using that instead. This seemed to work on the FC1 system, at least when LANG=en_US.UTF-8 -- this might depend on tcl's idea of the sytem encoding, though. Jeff Test program: from Tkinter import * t = Tk(); t.withdraw() s = StringVar(t) s.set(u"\xa3") print repr(s.get()) t.destroy() -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.python.org/pipermail/tkinter-discuss/attachments/20050527/0c2b5c9b/attachment.pgp