Quantcast
Channel: Lion's posterous
Viewing all articles
Browse latest Browse all 8

tkhelp - the tkinter support module

$
0
0

Peoples, I've started work on tkhelp.  This is a tkinter support module.  I'm actively looking for collaborators, so please email me (LionKimbro@gmail.com) or call me (206.427.2545) if you want to participate in some way..!

Here are the links of import:

Here's a little bit about what it does.

Here's a basic setup:

>>> import tkinter
>>> import tkhelp
>>> tkhelp.setup()
True
>>>

That that setup() call does for you, is initialize the global Tk() instance, and then withdraws the window that (for whatever reason) tkinter or tk automatically assumes you'll be wanting.

(If you did want that window, call tkhelp.setup(withdraw=False)instead).

Now, create a tkinter tree...

>>> toplevel = tkinter.Toplevel(name="toplevel")
>>> b = tkinter.Button(toplevel, text="b", name="b")
>>> b.pack()

And, ask to see the widget hierarchy:

>>> tkhelp.print_hierarchy()
** tk [6095814] <Tk> **                           200x200+0+0
     toplevel [6817202] <Toplevel>                116x26+25+25
       b [919100] <Button>                        18x26+49+0

("print_hierarchy()" is a bit wordy;  you can also just use "hr()".)

The number within the brackets [] is the tk widget ID#.  The string within <> is the tk widget type.  The values on the right are width,height,x,y.  The indentation shows the nesting.

There are also functions for easily retrieving widgets, and getting information about them.

>>> tkhelp.wid("b")
919100
>>> tkhelp.fullpath("b")
'.toplevel.b'
>>> tkhelp.name("b")
'b'
>>> tkhelp.wclass("b")
'Button'

Note that, in all of these examples, I used the tk "name" for the widget "b" to identify it.  But you can also just as easily use the ID for the widget, or the full path.

>>> tkhelp.fullpath(919100)
'.toplevel.b'
>>> tkhelp.wid(".toplevel.b")
919100

("wid" is short for "widget id."  I'd use "id", but it's a built-in Python function.)

Of course, you'll want to access the widgets themselves, as well.

>>> tkhelp.find(".")
<tkinter.Tk object at 0x0000000002554390>
>>> tkhelp.find("toplevel")
<tkinter.Toplevel object at 0x00000000026A4588>
>>> tkhelp.find("b")
<tkinter.Button object at 0x00000000026BDB00>

These are very, very rudimentary capabilities -- and yet, incredibly helpful if you are working with tkinter.

This module is all about making tkinter easy to use, without putting a heavy layer on top of tkinter itself.  I'd rather give increased visibility to what is going on in tkinter, than put a layer on top.

If you are interested in learning about where I am going with this, or interested in contributing, please consult the Google Doc I've created for this project, or leave comment on this post.  I can be reached also at LionKimbro@gmail.com, or 206.427.2545 (Seattle, USA).

Download the code at github.

 

Permalink | Leave a comment  »


Viewing all articles
Browse latest Browse all 8

Trending Articles