From ggpolo at gmail.com Mon Feb 2 05:21:31 2009 From: ggpolo at gmail.com (Guilherme Polo) Date: Mon, 2 Feb 2009 02:21:31 -0200 Subject: [Tkinter-discuss] Adding something in Tkinter to facilitate package loading Message-ID: Hi there, I was considering the possibility of adding something to facilitate package loading in Tkinter. If you have done a wrapper, or used one, you may have noticed that there is always some code like this: global _TKTABLE_LOADED if not _TKTABLE_LOADED: tktable_lib = os.environ.get('TKTABLE_LIBRARY') if tktable_lib: master.tk.eval('global auto_path; ' 'lappend auto_path {%s}' % tktable_lib) master.tk.call('package', 'require', 'Tktable') _TKTABLE_LOADED = True This one was taken from the tktable wrapper (as you can see), some details will change from wrapper to wrapper (but apparently not by much). There is also the possibility that the wrapper modifies Tkinter.Tk (see ttk), or subclass it (see Tix), to inject this kind of code -- which may be problematic. I see it as problematic when you want to test, lets say, a module P that depends on X and modifies Tkinter.Tk, and another module Q that depends on Y (but no X). When P is executed it does whatever it needs to load X, if loading X succeeds then it is all good, but lets suppose it failed. Now Q executes and tries instantiating Tkinter.Tk, the problem is that it doesn't depend on X to execute but P already modified it to load X (but it failed), now Q fails too. Maybe I'm just bringing this up because I decided to modify Tkinter.Tk on ttk, expected some form of problems initially, but started having real problems just now. Anyway, even if the second reason looks bad, I still would like to have something to load packages easily. I was thinking something like: tcl_deps = {} tk_deps = {} def add_pkgdep(ident, pkg_loader, depends_on='tk'): """Add a new package to be loaded after the interpreter is created. ident is just any identifier (that can be used as a dict key), to identify your pkg_loader. If another pkg_loader is given with the same ident then a DuplicatePkgIdentError is raised. pkg_loader should be a callable which takes a single argument. It will be called with a Tk instance. If depends_on is set to 'tk', then the package loader will be invoked after tk loads and only if the interpreter loads tk. """ if depends_on == 'tk': d = tk_deps else: d = tcl_deps if ident in tk_deps or ident in tcl_deps: raise DuplicatePkgIdentError("%r is already taken" % ident) d[ident] = pkg_loader def pop_pkgdep(ident): """Remove and return the package loader associated with ident from the dependencies.""" if ident in tk_deps: return tk_deps.pop(ident) return tcl_deps.pop(ident) def clear_pkgdeps(): """Clear all dependencies currently set.""" for item in (tcl_deps, tk_deps): item.clear() def basic_dep_loader(master, pkg_name, env_var=None): dep_path = os.environ.get(env_var) if dep_path: # append custom dependence path to the the list of directories that # Tcl uses when attempting to resolve packages with the package # command master.tk.eval( 'global auto_path; ' 'lappend auto_path {%s}' % dep_path) # TclError may be raised now master.tk.eval('package require %s' % pkg_name) Some very small modifications (two lines actually) would be needed in Tkinter.Tk to invoke the loaders at the right time, and wouldn't affect the current usage of Tkinter. Applying it in ttk, for example, would reduce this code (comments stripped): def _loadttk(loadtk): def _wrapper(self): loadtk(self) if _REQUIRE_TILE: import os tilelib = os.environ.get('TILE_LIBRARY') if tilelib: self.tk.eval('global auto_path; ' 'lappend auto_path {%s}' % tilelib) self.tk.eval('package require tile') # TclError may be raised here return _wrapper __loadtk__ = Tkinter.Tk._loadtk Tkinter.Tk._loadtk = _loadttk(Tkinter.Tk._loadtk) To this (besides making it easier to test): def _tile_loader(master): # Load tile if needed, this will happen right after Tk loads. if _REQUIRE_TILE: Tkinter.basic_dep_loader(master, 'tile', 'TILE_LIBRARY') Tkinter.add_pkgdep('tile', _tile_loader) What do you think about this addition ? Should the order of packages matter ? Add some introspection ? -- -- Guilherme H. Polo Goncalves From ggpolo at gmail.com Mon Feb 2 20:19:31 2009 From: ggpolo at gmail.com (Guilherme Polo) Date: Mon, 2 Feb 2009 17:19:31 -0200 Subject: [Tkinter-discuss] Adding something in Tkinter to facilitate package loading In-Reply-To: References: Message-ID: On Mon, Feb 2, 2009 at 2:21 AM, Guilherme Polo wrote: > Hi there, > > I was considering the possibility of adding something to facilitate > package loading in Tkinter. If you have done a wrapper, or used one, > you may have noticed that there is always some code like this: > > global _TKTABLE_LOADED > if not _TKTABLE_LOADED: > tktable_lib = os.environ.get('TKTABLE_LIBRARY') > if tktable_lib: > master.tk.eval('global auto_path; ' > 'lappend auto_path {%s}' % tktable_lib) > master.tk.call('package', 'require', 'Tktable') > _TKTABLE_LOADED = True > > This one was taken from the tktable wrapper (as you can see), some > details will change from wrapper to wrapper (but apparently not by > much). > > There is also the possibility that the wrapper modifies Tkinter.Tk > (see ttk), or subclass it (see Tix), to inject this kind of code -- > which may be problematic. I see it as problematic when you want to > test, lets say, a module P that depends on X and modifies Tkinter.Tk, > and another module Q that depends on Y (but no X). When P is executed > it does whatever it needs to load X, if loading X succeeds then it is > all good, but lets suppose it failed. Now Q executes and tries > instantiating Tkinter.Tk, the problem is that it doesn't depend on X > to execute but P already modified it to load X (but it failed), now Q > fails too. Maybe I'm just bringing this up because I decided to modify > Tkinter.Tk on ttk, expected some form of problems initially, but > started having real problems just now. > > Anyway, even if the second reason looks bad, I still would like to > have something to load packages easily. That first attempt went too fast from my part, I would say. I've re-though about it today and came up with this: http://tkinter.unpythonic.net/wiki/DependenceLoader dependences.load_now would be called right before Tkinter.Tk.__init__finishes. To add a tile dependence now I could do: dependences.add('tile', basic_preloader, env_var='TILE_LIBRARY') Which would be loaded when the Tcl intrerpreter is created. Hopefully someone would like something like this too, and if possible check if it would solve the problem for you too or if I'm thinking too much about myself -- and only myself. -- -- Guilherme H. Polo Goncalves From woodygar at sky.com Tue Feb 3 13:23:08 2009 From: woodygar at sky.com (Gary Wood) Date: Tue, 3 Feb 2009 12:23:08 -0000 Subject: [Tkinter-discuss] new to Python Message-ID: <00D78C71004D4DA69390AC538BFCCC51@Woodygar> any good tkinter tutorial for Python 3 Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From ggpolo at gmail.com Tue Feb 3 15:16:56 2009 From: ggpolo at gmail.com (Guilherme Polo) Date: Tue, 3 Feb 2009 12:16:56 -0200 Subject: [Tkinter-discuss] new to Python In-Reply-To: <00D78C71004D4DA69390AC538BFCCC51@Woodygar> References: <00D78C71004D4DA69390AC538BFCCC51@Woodygar> Message-ID: On Tue, Feb 3, 2009 at 10:23 AM, Gary Wood wrote: > any good tkinter tutorial for Python 3 Any for Python 2.x will do. The major difference in Python 3 is that modules were renamed, some were merged, and tkinter is a real package now, all this shouldn't affect the usefulness of existing tutorials and there is a brief description about each one you will find in Python 3.0 at http://docs.python.org/3.0/library/tkinter.html -- -- Guilherme H. Polo Goncalves From artencjo at o2.pl Mon Feb 9 22:01:13 2009 From: artencjo at o2.pl (artencjo) Date: Mon, 9 Feb 2009 13:01:13 -0800 (PST) Subject: [Tkinter-discuss] Possibly n00bish question about ImageTk In-Reply-To: <11330353.post@talk.nabble.com> References: <11330353.post@talk.nabble.com> Message-ID: <21921865.post@talk.nabble.com> Hello! I know I just dug out a message from over a year, but I had exactly the same problem with my python and solved it quite easily... Just use command mainloop() after the program renders image. It refreshes the whole window, filling the gray frame with my graph ;) Greetings, Artur Barnesdesdes wrote: > > I've been trying to make a program which will import a png file and > display it inside a frame. It actually works when I just run the code, > but if I try to abstract the exact same code into a function, it fails. > In the code below, if I just run it, it works. However, if I comment out > the lines between "root=Tkinter.Tk()" and "moo(root)" and comment in > "moo(root)", it no longer works. Instead of displaying the image, it just > displays a gray frame of the correct size. Does anyone have any insights > about what I'm doing wrong? > > import Image, ImageTk, Tkinter > > def moo(master): > img = Image.open('plotfig.png') > f = Tkinter.Frame(master, width=800, height=600) > pi = ImageTk.PhotoImage(img) > t = Tkinter.Label(f, image=pi) > f.pack() > t.place(x=0, y=0, width=800, height=600) > t.pack() > > root = Tkinter.Tk() > img = Image.open('plotfig.png') > f = Tkinter.Frame(root, width=800, height=600) > pi = ImageTk.PhotoImage(img) > t = Tkinter.Label(f, image=pi) > f.pack() > t.place(x=0, y=0, width=800, height=600) > t.pack() > #moo(root) > root.mainloop() > > > Thanks very much. > > Alan > > -- View this message in context: http://www.nabble.com/Possibly-n00bish-question-about-ImageTk-tp11330353p21921865.html Sent from the Python - tkinter-discuss mailing list archive at Nabble.com. From ggpolo at gmail.com Tue Feb 10 18:08:15 2009 From: ggpolo at gmail.com (Guilherme Polo) Date: Tue, 10 Feb 2009 15:08:15 -0200 Subject: [Tkinter-discuss] Possibly n00bish question about ImageTk In-Reply-To: <21921865.post@talk.nabble.com> References: <11330353.post@talk.nabble.com> <21921865.post@talk.nabble.com> Message-ID: On Mon, Feb 9, 2009 at 7:01 PM, artencjo wrote: > > Hello! > I know I just dug out a message from over a year, but I had exactly the same > problem with my python and solved it quite easily... > Just use command mainloop() after the program renders image. It refreshes > the whole window, filling the gray frame with my graph ;) > Greetings, > Artur > I didn't understand your solution, how do you know if the image has been rendered before calling mainloop ? Are you waiting for visibility on the label or something like that ? The more correct solution is to actually store a reference to the ImageTk.PhotoImage instance (named "pi" in the example below), otherwise the image will be deleted right after there are no more references to it, which in the example given below will happen after the function returns. I don't like this behaviour either, btw. > Barnesdesdes wrote: >> >> I've been trying to make a program which will import a png file and >> display it inside a frame. It actually works when I just run the code, >> but if I try to abstract the exact same code into a function, it fails. >> In the code below, if I just run it, it works. However, if I comment out >> the lines between "root=Tkinter.Tk()" and "moo(root)" and comment in >> "moo(root)", it no longer works. Instead of displaying the image, it just >> displays a gray frame of the correct size. Does anyone have any insights >> about what I'm doing wrong? >> >> import Image, ImageTk, Tkinter >> >> def moo(master): >> img = Image.open('plotfig.png') >> f = Tkinter.Frame(master, width=800, height=600) >> pi = ImageTk.PhotoImage(img) >> t = Tkinter.Label(f, image=pi) >> f.pack() >> t.place(x=0, y=0, width=800, height=600) >> t.pack() >> >> root = Tkinter.Tk() >> img = Image.open('plotfig.png') >> f = Tkinter.Frame(root, width=800, height=600) >> pi = ImageTk.PhotoImage(img) >> t = Tkinter.Label(f, image=pi) >> f.pack() >> t.place(x=0, y=0, width=800, height=600) >> t.pack() >> #moo(root) >> root.mainloop() >> >> >> Thanks very much. >> >> Alan >> >> > > -- > View this message in context: http://www.nabble.com/Possibly-n00bish-question-about-ImageTk-tp11330353p21921865.html > Sent from the Python - tkinter-discuss mailing list archive at Nabble.com. > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > http://mail.python.org/mailman/listinfo/tkinter-discuss > -- -- Guilherme H. Polo Goncalves From argo785 at gmail.com Fri Feb 13 23:17:05 2009 From: argo785 at gmail.com (John Anon) Date: Fri, 13 Feb 2009 17:17:05 -0500 Subject: [Tkinter-discuss] pack: difference between anchor and side options? (also anchor as a widget option) Message-ID: <93565a2c0902131417o42bc1bacqdb7ea124b66ffaaa@mail.gmail.com> What is the difference between `foo.pack(anchor=...)` and `foo.pack(side=...)`? Actually, regarding `anchor`, I see that you can also use it as an option when creating a widget (ex. `Button(parent, anchor=...)` ). What's the difference between using it with pack and using as a widget option? From ggpolo at gmail.com Wed Feb 18 04:04:52 2009 From: ggpolo at gmail.com (Guilherme Polo) Date: Wed, 18 Feb 2009 00:04:52 -0300 Subject: [Tkinter-discuss] pack: difference between anchor and side options? (also anchor as a widget option) In-Reply-To: <93565a2c0902131417o42bc1bacqdb7ea124b66ffaaa@mail.gmail.com> References: <93565a2c0902131417o42bc1bacqdb7ea124b66ffaaa@mail.gmail.com> Message-ID: On Fri, Feb 13, 2009 at 7:17 PM, John Anon wrote: > What is the difference between `foo.pack(anchor=...)` and `foo.pack(side=...)`? > With "side" you have only four option: top, right, bottom and left, with "anchor" you can use some combinations of n, w, s, e or use center (the default). You can also use anchor and side at same time, for example x.pack(side='top', anchor=''w'). side='top' is the default one, it defines where in the master the widget will be packed, anchor defines the position of the widget in this side you chose. > Actually, regarding `anchor`, I see that you can also use it as an > option when creating a widget (ex. `Button(parent, anchor=...)` ). > What's the difference between using it with pack and using as a widget > option? For using it with pack see the paragraph above. Now when you use the option "anchor" while creating a Button, it will be defining where its text and/or image will be placed in the widget, just like described above. Try creating a Button with some text, pack it with expand=True and fill='both' and change the value in anchor to see how it behaves. Anyway, by now (4 days late) you probably already found out all this :) Regards, -- -- Guilherme H. Polo Goncalves From slehar at gmail.com Sat Feb 21 23:05:20 2009 From: slehar at gmail.com (Steven Lehar) Date: Sat, 21 Feb 2009 17:05:20 -0500 Subject: [Tkinter-discuss] Tkinter in Netbeans? Message-ID: <30309f9e0902211405w4576bb7ch385b6214a6f35ee7@mail.gmail.com> Has anyone ported Tkinter over to Netbeans? That would be the perfect IDE environment! -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at passcal.nmt.edu Thu Feb 26 22:10:53 2009 From: bob at passcal.nmt.edu (Bob Greschke) Date: Thu, 26 Feb 2009 14:10:53 -0700 Subject: [Tkinter-discuss] Changing the cursor for all widgets Message-ID: If I have a Toplevel (or Root) window with a canvas on it it looks like changing the cursor for the window, to a wristwatch for example, also changes the cursor for the canvas, but if the window has a Text widget on it the cursor when mousing over the edge of the window changes to the wristwatch, but over the Text field it turns back into an insertion cursor (I-beam). Is there a way to get the Text()s to change along with everyone else, or do I just have to configure them separately? Thanks! Bob From ggpolo at gmail.com Thu Feb 26 22:44:19 2009 From: ggpolo at gmail.com (Guilherme Polo) Date: Thu, 26 Feb 2009 18:44:19 -0300 Subject: [Tkinter-discuss] Changing the cursor for all widgets In-Reply-To: References: Message-ID: On Thu, Feb 26, 2009 at 6:10 PM, Bob Greschke wrote: > If I have a Toplevel (or Root) window with a canvas on it it looks like > changing the cursor for the window, to a wristwatch for example, also > changes the cursor for the canvas, but if the window has a Text widget on it > the cursor when mousing over the edge of the window changes to the > wristwatch, but over the Text field it turns back into an insertion cursor > (I-beam). ?Is there a way to get the Text()s to change along with everyone > else, or do I just have to configure them separately? > Child widgets of this toplevel will only use the toplevel's cursor if they don't have a cursor defined for themselves. If you check the cursor defined in your text widget by doing "print textwidget['cursor']" you will see there is already a cursor defined for it, so you can either create this textwidget with cursor='' or set it to '' after creating. > Thanks! > > Bob > -- -- Guilherme H. Polo Goncalves From bob at passcal.nmt.edu Fri Feb 27 00:03:43 2009 From: bob at passcal.nmt.edu (Bob Greschke) Date: Thu, 26 Feb 2009 16:03:43 -0700 Subject: [Tkinter-discuss] Changing the cursor for all widgets In-Reply-To: References: Message-ID: <0C9F4B8F-BED2-4539-A537-2248910F0768@passcal.nmt.edu> OOoooOOooo! That's just right for these Text()s since they are not really for editing, but just for displaying stuff. I didn't know about the cursor='' trick. Perfect! Thanks! Bob On Feb 26, 2009, at 14:44, Guilherme Polo wrote: > On Thu, Feb 26, 2009 at 6:10 PM, Bob Greschke > wrote: >> If I have a Toplevel (or Root) window with a canvas on it it looks >> like >> changing the cursor for the window, to a wristwatch for example, also >> changes the cursor for the canvas, but if the window has a Text >> widget on it >> the cursor when mousing over the edge of the window changes to the >> wristwatch, but over the Text field it turns back into an insertion >> cursor >> (I-beam). Is there a way to get the Text()s to change along with >> everyone >> else, or do I just have to configure them separately? >> > > Child widgets of this toplevel will only use the toplevel's cursor if > they don't have a cursor defined for themselves. > > If you check the cursor defined in your text widget by doing "print > textwidget['cursor']" you will see there is already a cursor defined > for it, so you can either create this textwidget with cursor='' or set > it to '' after creating. > >> Thanks! >> >> Bob >> > > > > -- > -- Guilherme H. Polo Goncalves > From argo785 at gmail.com Sat Feb 28 23:52:00 2009 From: argo785 at gmail.com (John Anon) Date: Sat, 28 Feb 2009 17:52:00 -0500 Subject: [Tkinter-discuss] pack: difference between anchor and side options? (also anchor as a widget option) In-Reply-To: References: <93565a2c0902131417o42bc1bacqdb7ea124b66ffaaa@mail.gmail.com> Message-ID: <93565a2c0902281452y54e3911cp7795a8e024fb7633@mail.gmail.com> Guilherme, Sorry for the very late reply, and thank you very much for the information. So, `side` seems to have precedence over `anchor`. Very good. I'll experiment with it soon and try to make some time to add my findings and sample code to [the wiki](http://tkinter.unpythonic.net/wiki/). In the meantime, I added a LayoutManagement wiki page (linked to from the front page) along with a small amount of content. On Tue, Feb 17, 2009 at 10:04 PM, Guilherme Polo wrote: > On Fri, Feb 13, 2009 at 7:17 PM, John Anon wrote: >> What is the difference between `foo.pack(anchor=...)` and `foo.pack(side=...)`? >> > > With "side" you have only four option: top, right, bottom and left, > with "anchor" you can use some combinations of n, w, s, e or use > center (the default). You can also use anchor and side at same time, > for example x.pack(side='top', anchor=''w'). side='top' is the default > one, it defines where in the master the widget will be packed, anchor > defines the position of the widget in this side you chose. > >> Actually, regarding `anchor`, I see that you can also use it as an >> option when creating a widget (ex. `Button(parent, anchor=...)` ). >> What's the difference between using it with pack and using as a widget >> option? > > For using it with pack see the paragraph above. > Now when you use the option "anchor" while creating a Button, it will > be defining where its text and/or image will be placed in the widget, > just like described above. Try creating a Button with some text, pack > it with expand=True and fill='both' and change the value in anchor to > see how it behaves. > > Anyway, by now (4 days late) you probably already found out all this :) > > Regards, > > -- > -- Guilherme H. Polo Goncalves >