I'm trying to create a simple tabbed editor and merged two examples found on the web. One of those is the one with the closing images on the tabs. In the proces of understanding I somehowe lost the images on the tabs but not the fuctionality of clicking on the right position of the tabs.

What is going wrong?

TIA, Ingo

import os
from tkinter import *
from tkinter import ttk

class OPMLnotebook(Frame):

    def __init__(self):
        Frame.__init__(self)
        self.master.title('OPML Notebook')
        self._create_panel()

    def _tabbtn_press(self, event):
        x, y, widget = event.x, event.y, event.widget
        elem = widget.identify(x, y)
        index = widget.index("@%d,%d" % (x, y))
        if "close" in elem:
            widget.state(['pressed'])
            widget.pressed_index = index

    def _tabbtn_release(self, event):
        x, y, widget = event.x, event.y, event.widget
        if not widget.instate(['pressed']):
            return
        elem =  widget.identify(x, y)
        index = widget.index("@%d,%d" % (x, y))
        if "close" in elem and widget.pressed_index == index:
            widget.forget(index)
            widget.event_generate("<<NotebookClosedTab>>")
        widget.state(["!pressed"])
        widget.pressed_index = None

    def _create_panel(self):

        imgdir = os.path.join(os.path.dirname(__file__), 'img')
i1 = PhotoImage("img_close", file=os.path.join(imgdir, 'close.gif')) i2 = PhotoImage("img_closeactive", file=os.path.join(imgdir, 'close_active.gif')) i3 = PhotoImage("img_closepressed", file=os.path.join(imgdir, 'close_pressed.gif'))

        style = ttk.Style()
        style.element_create("close", "image", "img_close",
            ("active", "pressed", "!disabled", "img_closepressed"),
("active", "!disabled", "img_closeactive"), border=10, sticky='') style.layout("ButtonNotebook", [("ButtonNotebook.client", {"sticky": "nswe"})])
        style.layout("ButtonNotebook.Tab", [
            ("ButtonNotebook.tab", {"sticky": "nswe", "children":
[("ButtonNotebook.padding", {"side": "top", "sticky": "nswe",
                                             "children":
[("ButtonNotebook.label", {"side": "left", "sticky": ''}), ("ButtonNotebook.close", {"side": "left", "sticky": ''})]
                })]
            })]
        )

self.bind_class("TNotebook", "<ButtonPress-1>", self._tabbtn_press, True) self.bind_class("TNotebook", "<ButtonRelease-1>", self._tabbtn_release)

        nb = ttk.Notebook(width=400, height=400, style="ButtonNotebook")
        nb.pressed_index = None
        nb.enable_traversal()
        nb.pack(fill='both', expand=1)
        self._create_text_tab(nb)
        self._create_text_tab(nb)

    def _create_text_tab(self, nb):
        frame = Frame(nb)
        txt = Text(frame, wrap=WORD, width=40, height=10)
        vscroll = Scrollbar(frame, orient=VERTICAL, command=txt.yview)
        txt['yscroll'] = vscroll.set
        vscroll.pack(side=RIGHT, fill=Y)
        txt.pack(fill='both', expand=Y)
        nb.add(frame, text='Text Editor', underline=0)


if __name__ == '__main__':
    OPMLnotebook().mainloop()

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to