[Tkinter-discuss] Very interesting code sample: A Ttk Notebook with close buttons

python at bdurham.com python at bdurham.com
Wed Dec 15 22:22:02 CET 2010


In researching how to determine when a ttk.Notebook tab caption
has keyboard focus (still no luck), I stumbled across this code
snippet which allows you to add Firefox-like close controls to
your ttk.Notebook tabs. The close controls are added to the tab
components via styles - they are not grafted on widgets that are
place()-ed above controls.

Here's the copy and paste code for Python 3.1; the usual
Tkinter/ttk import statements need to be adjusted for Python 2.7.
http://www.java2s.com/Open-Source/Python/3.1.2-Python/Demo/Demo/t
kinter/ttk/notebook_closebtn.py.htm

Note: You need to supply 3 close button gif files. I created 3
16x16 solid gif files in MS Paint so I could see the code in use.

What is so cool about this example is that it uses the following
methods to "drill-into" the Notebook and child Tab widgets to
really customize the appearance and behavior of these widgets
(capabilities that I didn't think existed in ttk).

style.element_create( ... )
style.layout( ... )
elem = widget.identify(x, y) <--- allows one to examine child
components

I can't admit to understanding this code snippet, but I'm blown
away with the capabilities of ttk.

If anyone figures out this code, I would be curious how difficult
it would be to implement the following Firefox like behaviors:

- only show close buttons on the active tab vs. on all tabs
- show an arrow image above a tabs' left or right edge to
indicate the position of a dragged tab

Malcolm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20101215/b9a86877/attachment.html>


More information about the Tkinter-discuss mailing list