python - Is there a way to add close buttons to tabs in tkinter.ttk.Notebook? -


i want add close buttons each tab in tkinter.ttk.notebook. tried adding image , react click event unfortunately bitmapimage not have bind() method.

how can fix code?

#!/usr/binenv python3  tkinter import * tkinter.ttk import *   class application(tk):     def __init__(self):         super().__init__()         notebook = notebook(self)         notebook.pack(fill=both, expand=true)         self.img = bitmapimage(master=self, file='./image.xbm')         self.img.bind('<button-1>', self._on_click)         notebook.add(label(notebook, text='tab content'), text='tab caption', image=self.img)      def _on_click(self, event):         print('it works')  app = application() app.mainloop() 

image.xbm

#define bullet_width 11 #define bullet_height 9 static char bullet_bits = {     0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x00, 0xf8, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00 } 

one advantage of themed (ttk) widgets can create new widgets out of individual widget "elements". while not simple (nor documented), can create new "close tab" element add add "tab" element.

i present 1 possible solution. i'll admit it's not particularly easy understand. perhaps 1 of best sources how create custom widget styles can found @ tkdocs.com, starting styles , themes section.

try:     import tkinter tk     import ttk except importerror:  # python 3     import tkinter tk     tkinter import ttk  class customnotebook(ttk.notebook):     """a ttk notebook close buttons on each tab"""      __initialized = false      def __init__(self, *args, **kwargs):         if not self.__initialized:             self.__initialize_custom_style()             self.__inititialized = true          kwargs["style"] = "customnotebook"         ttk.notebook.__init__(self, *args, **kwargs)          self._active = none          self.bind("<buttonpress-1>", self.on_close_press, true)         self.bind("<buttonrelease-1>", self.on_close_release)      def on_close_press(self, event):         """called when button pressed on close button"""          element = self.identify(event.x, event.y)          if "close" in element:             index = self.index("@%d,%d" % (event.x, event.y))             self.state(['pressed'])             self._active = index      def on_close_release(self, event):         """called when button released on close button"""         if not self.instate(['pressed']):             return          element =  self.identify(event.x, event.y)         index = self.index("@%d,%d" % (event.x, event.y))          if "close" in element , self._active == index:             self.forget(index)             self.event_generate("<<notebooktabclosed>>")          self.state(["!pressed"])         self._active = none      def __initialize_custom_style(self):         style = ttk.style()         self.images = (             tk.photoimage("img_close", data='''                 r0lgodlhcaaiamibaaaaads7o4+pj9nz2ts7ozs7ozs7ozs7oyh+eunyzwf0zwqg                 d2l0acbhsu1qach5baekaaqalaaaaaaiaagaaamvgdbea0qnjygw7amxmuazhweu                 5kejads=                 '''),             tk.photoimage("img_closeactive", data='''                 r0lgodlhcaaiamieaaaaap/sap/bnnnz2cbgxsbgxsbgxsbgxih5baekaaqalaaa                 aaaiaagaaamvgdbea0qnjygw7amxmuazhweu5kejads=                 '''),             tk.photoimage("img_closepressed", data='''                 r0lgodlhcaaiamieaaaaaouqkv9mztnz2ts7ozs7ozs7ozs7oyh+eunyzwf0zwqg                 d2l0acbhsu1qach5baekaaqalaaaaaaiaagaaamvgdbea0qnjygw7amxmuazhweu                 5kejads=             ''')         )          style.element_create("close", "image", "img_close",                             ("active", "pressed", "!disabled", "img_closepressed"),                             ("active", "!disabled", "img_closeactive"), border=8, sticky='')         style.layout("customnotebook", [("customnotebook.client", {"sticky": "nswe"})])         style.layout("customnotebook.tab", [             ("customnotebook.tab", {                 "sticky": "nswe",                  "children": [                     ("customnotebook.padding", {                         "side": "top",                          "sticky": "nswe",                         "children": [                             ("customnotebook.focus", {                                 "side": "top",                                  "sticky": "nswe",                                 "children": [                                     ("customnotebook.label", {"side": "left", "sticky": ''}),                                     ("customnotebook.close", {"side": "left", "sticky": ''}),                                 ]                         })                     ]                 })             ]         })     ])  if __name__ == "__main__":     root = tk.tk()      notebook = customnotebook(width=200, height=200)     notebook.pack(side="top", fill="both", expand=true)      color in ("red", "orange", "green", "blue", "violet"):         frame = tk.frame(notebook, background=color)         notebook.add(frame, text=color)      root.mainloop() 

here's looks on linux system:

enter image description here


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -