From samuel.yin at 163.com Wed Nov 2 08:13:10 2005 From: samuel.yin at 163.com (Samuel Yin) Date: Wed, 02 Nov 2005 15:13:10 +0800 Subject: [Tkinter-discuss] No Parameters fo Callback functions? Message-ID: <43686706.1020905@163.com> An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20051102/9bbf6d28/attachment.htm From stewart.midwinter at gmail.com Wed Nov 2 18:17:17 2005 From: stewart.midwinter at gmail.com (Stewart Midwinter) Date: Wed, 2 Nov 2005 10:17:17 -0700 Subject: [Tkinter-discuss] No Parameters fo Callback functions? In-Reply-To: <43686706.1020905@163.com> References: <43686706.1020905@163.com> Message-ID: Samuel, you can use lambda for this purpose. Here's how you would use lambda in a menu to specify arguments to pass to a callback method: command = lambda self=self, viewer=viewer: self.open_pw_display(viewer, self.pwDisplays[viewer]) Note that you have to pass in a reference to the class you are in, using 'self', or the lambda won't find your method. HTH, S -- Stewart Midwinter stewart at midwinter.ca stewart.midwinter at gmail.com Skype, GoogleTalk, iChatAV, MSN, Yahoo: midtoad AIM:midtoad1 From mfranklin1 at gatwick.westerngeco.slb.com Fri Nov 4 10:01:06 2005 From: mfranklin1 at gatwick.westerngeco.slb.com (Martin Franklin) Date: Fri, 04 Nov 2005 09:01:06 +0000 Subject: [Tkinter-discuss] No Parameters fo Callback functions? In-Reply-To: References: <43686706.1020905@163.com> Message-ID: Stewart Midwinter wrote: > Samuel, you can use lambda for this purpose. Here's how you would > use lambda in a menu to specify arguments to pass to a callback > method: > > command = lambda self=self, viewer=viewer: > self.open_pw_display(viewer, self.pwDisplays[viewer]) > > Note that you have to pass in a reference to the class you are in, > using 'self', or the lambda won't find your method. > > HTH, The 'standard alternative' to lambda is to use a callback class like so :) class Callback: def __init__(self, checkbutton, action): self.checkbutton = checkbutton self.action = action def __call__(self): self.action(self.checkbutton) ... cb1 = Checkbutton(self, command=Callback("checkbutton1", self.actionFunction, etc...) cb2 = Checkbutton(self, command=Callback("checkbutton2", self.actionFunction, etc...) HTH Martin From fredrik at pythonware.com Fri Nov 4 10:28:27 2005 From: fredrik at pythonware.com (Fredrik Lundh) Date: Fri, 4 Nov 2005 10:28:27 +0100 Subject: [Tkinter-discuss] No Parameters fo Callback functions? References: <43686706.1020905@163.com> Message-ID: Martin Franklin wrote: > The 'standard alternative' to lambda is to use a callback class > like so :) > > class Callback: > def __init__(self, checkbutton, action): > self.checkbutton = checkbutton > self.action = action > > def __call__(self): > self.action(self.checkbutton) I strongly recommend using local function objects instead; the code is straightforward, they're cheap to create, and they give you a lot more flexibility the day you need to add more functionality to your callbacks. def callback(): self.actionFunction("checkButton1") cb1 = Checkbutton(self, command=callback) def callback(): self.actionFunction("checkButton2") cb2 = Checkbutton(self, command=callback) or, in a loop # create ten buttons for i in range(10): def callback(i=i): # <-- note: bind to value, not name self.actionFunction("checkButton%d" % i) cb = Checkbutton(self, text="Button %d" % i, command=callback) cb.pack() (the i=i construct is used to bind to the *current* value of the i variable, rather than whatever value it has when you leave the current scope) From mfranklin1 at gatwick.westerngeco.slb.com Fri Nov 4 11:53:54 2005 From: mfranklin1 at gatwick.westerngeco.slb.com (Martin Franklin) Date: Fri, 04 Nov 2005 10:53:54 +0000 Subject: [Tkinter-discuss] No Parameters fo Callback functions? In-Reply-To: References: <43686706.1020905@163.com> Message-ID: Fredrik Lundh wrote: > Martin Franklin wrote: > > >>The 'standard alternative' to lambda is to use a callback class >>like so :) >> >>class Callback: >> def __init__(self, checkbutton, action): >> self.checkbutton = checkbutton >> self.action = action >> >> def __call__(self): >> self.action(self.checkbutton) > > > I strongly recommend using local function objects instead; the code Indeed, I should have said one of the standard alternatives :) for whatever reason I seem to find the callback class more readable perhaps I just came across it first... > is straightforward, they're cheap to create, and they give you a lot > more flexibility the day you need to add more functionality to your > callbacks. > > def callback(): > self.actionFunction("checkButton1") > cb1 = Checkbutton(self, command=callback) > > def callback(): > self.actionFunction("checkButton2") > cb2 = Checkbutton(self, command=callback) > > or, in a loop > > # create ten buttons > for i in range(10): > def callback(i=i): # <-- note: bind to value, not name > self.actionFunction("checkButton%d" % i) > cb = Checkbutton(self, text="Button %d" % i, command=callback) > cb.pack() > > (the i=i construct is used to bind to the *current* value of the i > variable, rather than whatever value it has when you leave the > current scope) > > From geon at post.cz Sun Nov 6 06:35:37 2005 From: geon at post.cz (Pavel Kosina) Date: Sun, 06 Nov 2005 06:35:37 +0100 Subject: [Tkinter-discuss] separation gui from logic Message-ID: <436D9629.7090902@post.cz> In Tkinter I try put to gui design into one file, and in another file do: import gui design file and the script logic itself. I am not much successful, especially at callback functions, that are included in gui design files, f.e. Button(text='Tisk', command=hello), and refer to functions that exist in the main script. Is it somehow possible to do this? Not just this example of Button (how to solve reffering to function that not exists yet, and exists in "upper" program only), but this whole idea of separation.... Thank you Pavel Kosina From klappnase at web.de Wed Nov 9 11:52:49 2005 From: klappnase at web.de (Michael Lange) Date: Wed, 9 Nov 2005 11:52:49 +0100 Subject: [Tkinter-discuss] Ann.: Python wrapper for tktreectrl Message-ID: <20051109115249.50023b9e.klappnase@web.de> Hi, I wrote a Tkinter wrapper for the tktreectrl widget (http://tktreectrl.sourceforge.net). The treectrl widget is an advanced tool that lets you set up things like sortable multi column listboxes and tree browsers. The python module comes with a reference manual and a (very basic) demo. The url is . It requires Python-2.2 or higher. Any comments are much appreciated. Regards Michael From klappnase at web.de Mon Nov 14 21:59:54 2005 From: klappnase at web.de (Michael Lange) Date: Mon, 14 Nov 2005 21:59:54 +0100 Subject: [Tkinter-discuss] Convert Tk command string to Python callback Message-ID: <20051114215954.07accbed.klappnase@web.de> Hello everyone, I need to to pass attributes of an event (or maybe the event instance as a whole) to another callback. A pseudo-code function to illustrate what I want to achieve: def deliver_event(self, event): binding = self.tag_bind(tagOrId, '') if binding: binding(event) Of course this won't work because "binding" is a string that looks like: if {"[-1215579924drag_leave_canv %A %a %b %C %c %D %d %m %T %t %W %X %x %Y %y]" == "break"} break I just wondered if there is a way to replace the percent substitutions with the attributes of the event or (even better) to access the associated python callback from the tk command string. Any pointers are much appreciated. Thanks in advance Michael From rowen at cesmail.net Wed Nov 16 20:45:17 2005 From: rowen at cesmail.net (Russell E. Owen) Date: Wed, 16 Nov 2005 11:45:17 -0800 Subject: [Tkinter-discuss] Convert Tk command string to Python callback References: <20051114215954.07accbed.klappnase@web.de> Message-ID: In article <20051114215954.07accbed.klappnase at web.de>, Michael Lange wrote: > ...I just wondered if there is a way to ...access the associated python callback from the tk command > string... I hope this helps. I've appended code that registers python functions to be called from tcl/tk. This is from my RO python package , from RO.TkUtil. -- Russell class TclFunc: """Register a python function as a tcl function. Based on Tkinter's _register method (which, being private, I prefer not to use explicitly). If the function call fails, a traceback is printed. Please call deregister when you no longer want the tcl function to exist. """ tkApp = None def __init__(self, func, debug=False): if self.tkApp == None: self.tkApp = _getTkWdg().tk self.func = func self.tclFuncName = "pyfunc%s" % (id(self),) self.debug = bool(debug) try: self.tclFuncName += str(func.__name__) except AttributeError: pass if self.debug: print "registering tcl function %s for python function %s" % (self.tclFuncName, func) self.tkApp.createcommand(self.tclFuncName, self) def __call__(self, *args): try: self.func(*args) except (SystemExit, KeyboardInterrupt): raise except Exception, e: sys.stderr.write("tcl function %s failed: %s\n" % (self.tclFuncName, e)) traceback.print_exc(file=sys.stderr) def deregister(self): """Deregister callback and delete reference to python function. Safe to call if already deregistered. """ if self.debug: print "%r.deregister()" % (self,) if not self.func: if self.debug: print "already deregistered" return try: self.tkApp.deletecommand(self.tclFuncName) except Tkinter.TclError, e: if self.debug: print "deregistering failed: %r" % (e,) pass self.func = None def __repr__(self): return "%s(%s)" % (self.__class__.__name__, self.tclFuncName) def __str__(self): return self.tclFuncName def _getTkWdg(): """Return a Tk widget""" global g_tkWdg if not g_tkWdg: g_tkWdg = Tkinter.Frame() return g_tkWdg From Cameron at Phaseit.net Thu Nov 17 02:12:24 2005 From: Cameron at Phaseit.net (Cameron Laird) Date: Thu, 17 Nov 2005 01:12:24 +0000 Subject: [Tkinter-discuss] Convert Tk command string to Python callback In-Reply-To: References: <20051114215954.07accbed.klappnase@web.de> Message-ID: <20051117011224.GA11488@lairds.us> I've forgotten how to edit Wiki pages. More precisely, I don't know what to do so the Tkinter Wiki pages appear to me as other- than-immutable. Who can help? Jeff? From Cameron at Phaseit.net Thu Nov 17 02:29:30 2005 From: Cameron at Phaseit.net (Cameron Laird) Date: Thu, 17 Nov 2005 01:29:30 +0000 Subject: [Tkinter-discuss] separation gui from logic In-Reply-To: <436D9629.7090902@post.cz> References: <436D9629.7090902@post.cz> Message-ID: <20051117012930.GA12831@lairds.us> On Sun, Nov 06, 2005 at 06:35:37AM +0100, Pavel Kosina wrote: . . . > In Tkinter I try put to gui design into one file, and in another file > do: import gui design file and the script logic itself. > I am not much successful, especially at callback functions, that are > included in gui design files, f.e. Button(text='Tisk', command=hello), > and refer to functions that exist in the main script. > > Is it somehow possible to do this? Not just this example of Button (how > to solve reffering to function that not exists yet, and exists in > "upper" program only), but this whole idea of separation.... . . . Yes. I had hopes that Fredrik or John or Jeff or someone would speak up. I suspect there's already a good explanation of this subject written somewhere. If none turns up in the next few days, I'll write a bit on it. I'm sorry you've had to wait so long for help. From jepler at unpythonic.net Thu Nov 17 03:18:07 2005 From: jepler at unpythonic.net (jepler@unpythonic.net) Date: Wed, 16 Nov 2005 20:18:07 -0600 Subject: [Tkinter-discuss] Convert Tk command string to Python callback In-Reply-To: <20051117011224.GA11488@lairds.us> References: <20051114215954.07accbed.klappnase@web.de> <20051117011224.GA11488@lairds.us> Message-ID: <20051117021807.GB18024@unpythonic.net> On Thu, Nov 17, 2005 at 01:12:24AM +0000, Cameron Laird wrote: > I've forgotten how to edit Wiki pages. More precisely, I don't > know what to do so the Tkinter Wiki pages appear to me as other- > than-immutable. Who can help? Jeff? It looks like something is wrong on my end. I migrated the Tkinter wiki to a new machine a few weeks ago, and never bothered trying to log into the wiki after the change. It should be sufficient to log in to the wiki to be able to edit pages. I hope to have things fixed shortly, but right now I can't see the problem. 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/20051116/0497cd4b/attachment.pgp From jepler at unpythonic.net Thu Nov 17 03:21:52 2005 From: jepler at unpythonic.net (jepler@unpythonic.net) Date: Wed, 16 Nov 2005 20:21:52 -0600 Subject: [Tkinter-discuss] Convert Tk command string to Python callback In-Reply-To: <20051117021807.GB18024@unpythonic.net> References: <20051114215954.07accbed.klappnase@web.de> <20051117011224.GA11488@lairds.us> <20051117021807.GB18024@unpythonic.net> Message-ID: <20051117022151.GC18024@unpythonic.net> I still don't understand the problem, but I blew away the database of wiki users and was able to create a new user. You'll have to do the same before editing pages. Pleas let me know if it still doesn't work for you. Thanks, 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/20051116/3f7cd3a3/attachment.pgp From mfranklin1 at gatwick.westerngeco.slb.com Thu Nov 17 10:32:53 2005 From: mfranklin1 at gatwick.westerngeco.slb.com (Martin Franklin) Date: Thu, 17 Nov 2005 09:32:53 +0000 Subject: [Tkinter-discuss] separation gui from logic In-Reply-To: <20051117012930.GA12831@lairds.us> References: <436D9629.7090902@post.cz> <20051117012930.GA12831@lairds.us> Message-ID: Cameron Laird wrote: > On Sun, Nov 06, 2005 at 06:35:37AM +0100, Pavel Kosina wrote: > . > . > . > >>In Tkinter I try put to gui design into one file, and in another file >>do: import gui design file and the script logic itself. >>I am not much successful, especially at callback functions, that are >>included in gui design files, f.e. Button(text='Tisk', command=hello), >>and refer to functions that exist in the main script. >> >>Is it somehow possible to do this? Not just this example of Button (how >>to solve reffering to function that not exists yet, and exists in >>"upper" program only), but this whole idea of separation.... > > . > . > . > Yes. > > I had hopes that Fredrik or John or Jeff or someone would speak up. > I suspect there's already a good explanation of this subject written > somewhere. If none turns up in the next few days, I'll write a bit > on it. > > I'm sorry you've had to wait so long for help. I saw this message (or one just like it) in the python list so very sorry for not replying sooner. I don't think I've come across an answer on the net but one way you could do it is by passing the callbacks in as a dictionary like so:- import Tkinter class GUIDesign(Tkinter.Tk): def __init__(self, parent, callbacks={}): Tkinter.Tk.__init__(self) self.parent = parent button1 = Tkinter.Button(self, text = "Button1", command=callbacks["button1"]) button1.pack() class Main: def __init__(self): ## draw the GUI cbacks = {"button1" : self.button1_callback} gui = GUIDesign(self, cbacks) gui.mainloop() def button1_callback(self): print "Button 1 pressed" Main() The only drawback (as far as I can see) is that every time the GUI changes (a new button is added) you must also update the callbacks dictionary. I'm not so sure this is the 'best' solution, but it is 'a' solution :) Cheers Martin From klappnase at web.de Thu Nov 17 16:47:11 2005 From: klappnase at web.de (Michael Lange) Date: Thu, 17 Nov 2005 16:47:11 +0100 Subject: [Tkinter-discuss] Convert Tk command string to Python callback In-Reply-To: References: <20051114215954.07accbed.klappnase@web.de> Message-ID: <20051117164711.7b505cf8.klappnase@web.de> On Wed, 16 Nov 2005 11:45:17 -0800 "Russell E. Owen" wrote: > In article <20051114215954.07accbed.klappnase at web.de>, > Michael Lange wrote: > > > ...I just wondered if there is a way to ...access the associated python callback from the tk command > > string... > > I hope this helps. I've appended code that registers python functions to > be called from tcl/tk. This is from my RO python package > , from RO.TkUtil. > Thanks Russell, In the meantime I jfound another way to achieve what I want; I store the python comands in a _callbacks dictionary with the tk command strings as keys. I had to write a hacked version of _bind() for that, but I had to do so anyway. After the line cmd = ('if {"[%s %s]" == "break"} break\n' % (funcid, self._subst_format_str_dnd)) in _bind() I added self._callbacks[cmd] = func so I can do now something like: def deliver_event(self, event, sequence): res = 'break' tclCmd = self.bind(sequence) try: callback = self._callbacks[tclCmd] res = callback(event) except KeyError: pass return res Then I can write handlers for several events like this: def deliver_sequence1(self, event): return self.deliver_event(event, sequence1) This seems to work pretty well. Regards Michael From geon at post.cz Fri Nov 18 19:31:08 2005 From: geon at post.cz (geon) Date: Fri, 18 Nov 2005 19:31:08 +0100 Subject: [Tkinter-discuss] xscrollbar Message-ID: <437E1DEC.4010808@post.cz> I am not able to make x Scrollbar working. What is wrong? Thank you. from Tkinter import * root = Tk() scrollbar = Scrollbar(root, orient=HORIZONTAL) scrollbar.pack(side=BOTTOM, fill=X) text = Text(root, xscrollcommand=scrollbar.set) for i in range(1000): text.insert(END, str(i)) text.pack(side=LEFT, fill=BOTH, expand=YES) scrollbar.config(command=text.xview) mainloop() -- geon Vyj?mka je pravidlo. Rekurzivn?. From m_tayseer82 at yahoo.com Fri Nov 18 20:26:10 2005 From: m_tayseer82 at yahoo.com (Mohammad Tayseer) Date: Fri, 18 Nov 2005 11:26:10 -0800 (PST) Subject: [Tkinter-discuss] xscrollbar In-Reply-To: <437E1DEC.4010808@post.cz> Message-ID: <20051118192610.27116.qmail@web31109.mail.mud.yahoo.com> Text widget wraps the text. the solution is to change the third line to text = Text(root, wrap='none', xscrollcommand=scrollbar.set) from Tkinter import * root = Tk() scrollbar = Scrollbar(root, orient=HORIZONTAL) scrollbar.pack(side=BOTTOM, fill=X) text = Text(root, xscrollcommand=scrollbar.set) for i in range(1000): text.insert(END, str(i)) text.pack(side=LEFT, fill=BOTH, expand=YES) scrollbar.config(command=text.xview) mainloop() --------------------------------- Yahoo! FareChase - Search multiple travel sites in one click. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20051118/651e969a/attachment.htm From rowen at cesmail.net Mon Nov 21 22:06:30 2005 From: rowen at cesmail.net (Russell E. Owen) Date: Mon, 21 Nov 2005 13:06:30 -0800 Subject: [Tkinter-discuss] separation gui from logic References: <436D9629.7090902@post.cz> Message-ID: In article <436D9629.7090902 at post.cz>, Pavel Kosina wrote: > In Tkinter I try put to gui design into one file, and in another file > do: import gui design file and the script logic itself. > I am not much successful, especially at callback functions, that are > included in gui design files, f.e. Button(text='Tisk', command=hello), > and refer to functions that exist in the main script. > > Is it somehow possible to do this? Not just this example of Button (how > to solve reffering to function that not exists yet, and exists in > "upper" program only), but this whole idea of separation.... Consider coding the logic as methods of an object. Create an instance of this and pass it to your GUI class at instantiation. Your GUI class then references the relevant methods for its callbacks. I use this technique for separating data and the GUI (e.g. model-view-controller pattern). The model consists of one or more objects that contain the data. The GUI can then query and update the model as appropriate. I usually mix the logic (controller) in with the GUI and model, but it can also be a separate object. -- Russell