我的程序中有一个tkinter.ttk.notebook
,其中所有创建的选项卡都有close buttons
但我希望创建的第一个选项卡不应带有关闭按钮,其作用类似于用户无法关闭的欢迎选项卡
我有一个ttk笔记本,我有一个欢迎标签,我总是想在那里,没有关闭按钮,但我也希望其他标签有关闭按钮。此外,我所说的关闭按钮来自[本帖][1]
因此,如果有一种方法可以使关闭按钮只针对某些选项卡,而不是所有选项卡,那么它可能会有所帮助。 不幸的是,我在post中找到了关闭按钮的逻辑/代码,但它没有提到为某些选项卡设置关闭按钮的方法,而不是为其他选项卡设置关闭按钮的方法
我的代码-:
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
return "break"
def on_close_release(self, event):
"""Called when the button is released"""
if not self.instate(['pressed']):
return
element = self.identify(event.x, event.y)
if "close" not in element:
# user moved the mouse off of the close button
return
index = self.index("@%d,%d" % (event.x, event.y))
if 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)
welcome_frame = tk.Frame(notebook)
# I want this tab to have no close button
notebook.add(welcome_frame, text = 'Welcome')
for color in ("red", "orange", "green", "blue", "violet"):
frame = tk.Frame(notebook, background=color)
notebook.add(frame, text=color)
root.mainloop()
目前没有回答
相关问题 更多 >
编程相关推荐