不久前我在这里发现了允许在tkinter中的笔记本选项卡上添加关闭按钮的代码,但是我希望能够将常规笔记本(没有关闭按钮)与自定义笔记本结合起来。当我试着这样做的时候,我得到了一组常规笔记本的标签,然后在它下面是另一组定制笔记本的标签
正如所建议的,我已经包括了下面的图片,显示了目前发生的事情,但我要找的是有所有的标签在一起(其中红色将与橙色,绿色,蓝色等标签线),但不会有x允许它被关闭
我一直试图修改的代码是:
import tkinter as tk
import tkinter.ttk as 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()
note = ttk.Notebook(width=200, height=200)
note.pack(side='top', fill='both', expand=True)
notebook = CustomNotebook(width=200, height=200)
notebook.pack(side='top', fill='both', expand=True)
for color in ('red', 'orange', 'green', 'blue', 'violet'):
if color == 'red':
frame = tk.Frame(note, background=color)
note.add(frame, text=color)
else:
frame = tk.Frame(notebook, background=color)
notebook.add(frame, text=color)
root.mainloop()
我猜我必须使用自定义笔记本,并导致其中一个选项卡不使用关闭功能,但我不知道如何实现这一点。非常感谢您的帮助
我还要感谢bryanoakley提供了CustomNotebook类,我在下面的帖子中找到了这个类:Is there a way to add close buttons to tabs in tkinter.ttk.Notebook?
目前没有回答
相关问题 更多 >
编程相关推荐