2016-09-12 3 views
0

tkinter.ttk.Notebook의 각 탭에 닫기 버튼을 추가하고 싶습니다. 이미지 추가를 시도하고 클릭 이벤트에 반응했지만 불행히도 BitmapImage에는 bind() 메서드가 없습니다.tkinter.ttk.Notebook의 탭에 닫기 버튼을 추가하는 방법이 있습니까?

이 코드는 어떻게 수정합니까?

#!/usr/binenv python3 

from tkinter import * 
from 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 테마가 (TTK) 위젯의

#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 
} 

답변

5

한 가지 장점은 개별 위젯 "요소"에서 새로운 위젯을 만들 수 있다는 것입니다. 정확하게 단순하지는 않지만 (잘 문서화되어 있지는 않지만) 새로운 "닫기 탭"요소를 추가하여 "탭"요소에 추가합니다.

나는 가능한 해결책을 제시 할 것이다. 특히 이해하기 쉽지 않다는 것을 인정합니다. 아마도 맞춤형 위젯 스타일을 만드는 가장 좋은 방법 중 하나는 Styles and Themes 섹션부터 tkdocs.com에서 찾을 수 있습니다.

try: 
    import Tkinter as tk 
    import ttk 
except ImportError: # Python 3 
    import tkinter as tk 
    from tkinter import ttk 

class CustomNotebook(ttk.Notebook): 
    """A ttk Notebook with 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 the button is pressed over the 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 the button is released over the 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 and 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) 

    for color in ("red", "orange", "green", "blue", "violet"): 
     frame = tk.Frame(notebook, background=color) 
     notebook.add(frame, text=color) 

    root.mainloop() 

는 여기가 리눅스 시스템에 어떻게 표시되는지를 보여줍니다 :

enter image description here

+0

각 탭에 대한 새로운 요소를 고유하게하는 방법 하드는 것입니까? 내 응용 프로그램에서 "닫기"요소와 "저장/수정 된"요소가 필요합니다. 내용이 현재 저장되지 않았거나 저장 될 때 빨간색 또는 회색으로 표시됩니다 (메모장 + + 에서처럼). 내가 직면하고있는 문제는 각 탭에서 "저장"요소 색상을 제어하는 ​​방법입니다. 따라서 여기에서 답을 구할 때 "닫기"요소를 클릭하면 "닫기"요소의 색상을 어떻게 바꿀 수 있습니까? (물론 탭/자식을 닫지 말고) – Gary02127

+0

좋아, 방금 테마를 말한 https://stackoverflow.com/questions/23038356/change-color-of-tab-header-in-ttk-notebook을 찾았습니다. 스타일 대신. 여전히 그것을 소화하려고 노력하고 있습니다 ... 물론이 예제는 추가 요소의 추가 복잡성을 다루지 않습니다 ... – Gary02127

관련 문제