El código es demasiado largo no merece la pena copiarlo aquí .. Gracias Un saludo
Este es el codigo lo mas resumido posible:
Esta es la aplicacion principal, con una pestaña, unos botones que abren mas pestañas, la idea es añadir X pestañas posibles, el problea esta cuando intenta añadir pestañas desdeun boton en parte superior que dice ABRIR.
from tkinter import *
from tkinter import ttk
from ScrollableNotebook import *
class Aplicacion():
def __init__(self):
self.root = Tk()
self.root.title("Example")
self.notebook = ScrollableNotebook(
self.root, wheelscroll=True, tabmenu=True)
frame1 = Frame(self.notebook)
self.notebook.add(frame1, text="WorkSpace")
self.notebook.pack(fill="both", expand=True)
text = Text(frame1)
text.pack()
Label(frame1, text="Pantalla PRINCIPAL").pack()
Button(frame1,
text="Issues EXTRACIONES",
command=self.abrir_ISSETX,
).pack()
Button(frame1,
text="Issues DESVIACIONES",
command=self.abrir_ISSDES,
).pack()
def abrir_ISSETX(self):
self.cuaderno = Frame(self.notebook)
self.notebook.add(self.cuaderno, text="Issues EXTRACIONES")
def abrir_ISSDES(self):
self.cuaderno = Frame(self.notebook)
self.notebook.add(self.cuaderno, text="Issues DESVIACIONES")
def mainloop(self):
self.root.mainloop()
if __name__ == "__main__":
app = Aplicacion()
app.mainloop()
Ahora el segundo fichero que es un codigo de una persona que publico, como añadir la X en las pestañas, desde este archivo que lo he configurado a mi gusto, sin tocar la licencia, he añadido una funcion que me llama a la funcion del archivo principal:
He probado varias formas de abrir una nueva pestaña y os paso esta por escribir algo.
def _abrir_issuesEXT(self):
from Example import Aplicacion
Aplicacion.abrir_ISSETX(Aplicacion)
# -*- coding: utf-8 -*-
# Copyright (c) Muhammet Emin TURGUT 2020
# For license see LICENSE
from posixpath import commonpath
import tkinter as tk
import tkinter.font as tkFont
from tkinter import font
import os
import time
from tkinter import *
from tkinter import ttk
from threading import Thread
from PIL import Image, ImageTk
release = True
path = os.path.expanduser("~/")
path_icon = path+"Compliance/image/"
count = 0
class ScrollableNotebook(ttk.Frame):
_initialized = False
def __init__(self,parent,wheelscroll=False,tabmenu=False,*args,**kwargs):
ttk.Frame.__init__(self, parent, *args)
if not self._initialized:
self._initialize()
self._inititialized = True
kwargs["style"] = "ScrollableNotebook"
self._active = None
self.xLocation = 0
self.WorkSpac_icon = ImageTk.PhotoImage(Image.open(path_icon+r"workspace.png").resize((20, 20)))
#self.novo = ImageTk.PhotoImage(Image.open(path_icon+r"novo.png").resize((25, 25)))
self.notebookContent = ttk.Notebook(self,**kwargs)
self.notebookContent.pack(fill="both", expand=True)
self.notebookTab = ttk.Notebook(self,**kwargs)
self.notebookTab.bind("<<NotebookTabChanged>>",lambda e:self._tabChanger(e))
if wheelscroll==True:
self.notebookTab.bind("<MouseWheel>", self._wheelscroll)
self.notebookTab.bind("<Button-4>", self._wheelscroll)
self.notebookTab.bind("<Button-5>", self._wheelscroll)
slideFrame = ttk.Frame(self)
slideFrame.place(relx=1.0, x=0, y=1, anchor=NE)
self.menuSpace=30
if tabmenu==True:
self.menuSpace=50
self.bottomTab = ttk.Label(slideFrame,
text=" \u2630 ",
background='#DF2E2E',
foreground='#F6D167',
width=5,
anchor="center"
)
self.bottomTab.bind("<1>",self._bottomMenu)
self.bottomTab.pack(side=RIGHT, ipady=14)
self.bottomTab_novo = ttk.Label(slideFrame,
#image=self.novo,
text="Abrir",
width=5,
background="#082032",
foreground="white",
)
self.bottomTab_novo.bind("<1>",self._bottomMenu_novo)
self.bottomTab_novo.pack(side=LEFT, ipady=14)
self.leftArrow = ttk.Label(slideFrame,
text=" \u276E ",
foreground="#297F87",
)
self.leftArrow.bind("<Button-1>",lambda e: Thread(target=self._leftSlide, daemon=True).start())
self.leftArrow.bind("<ButtonRelease-1>", self._release_callback)
self.leftArrow.pack(side=LEFT, ipady=14)
self.rightArrow = ttk.Label(slideFrame,
text=" \u276F ",
foreground="#297F87",
)
#rightArrow.bind("<1>",self._rightSlide)
self.rightArrow.bind("<Button-1>",lambda e: Thread(target=self._rightSlide, daemon=True).start())
self.rightArrow.bind("<ButtonRelease-1>", self._release_callback)
self.rightArrow.pack(side=RIGHT, ipady=10)
self.notebookContent.bind("<Configure>", self._resetSlide)
self.notebookTab.bind("<ButtonPress-1>", self.on_tab_close_press, True)
self.notebookTab.bind("<ButtonRelease-1>", self.on_tab_close_release)
self.notebookContent.bind("<ButtonPress-1>", self.on_tab_close_press, True)
self.notebookContent.bind("<ButtonRelease-1>", self.on_tab_close_release)
def _release_callback(self, e):
global release
release = True
self.rightArrow.configure(foreground='#297F87')
self.leftArrow.configure(foreground='#297F87')
def on_tab_close_press(self, event):
name = self.identify(event.x, event.y)
if name == "tab_btn_close":
index = self.index("@%d,%d" % (event.x, event.y))
self.state(['pressed'])
self._active = index
def on_tab_close_release(self, event):
if not self.instate(['pressed']):
return None
name = self.identify(event.x, event.y)
if name == "tab_btn_close":
index = self.index("@%d,%d" % (event.x, event.y))
if index != 0:
if self._active == index:
self.forget(index)
self.notebookContent.forget(index)
self.event_generate("<<NotebookTabClosed>>")
self.state(["!pressed"])
self._active = None
def _initialize(self):
self.style = ttk.Style()
self.images = (
tk.PhotoImage("img1", data='''
iVBORw0KGgoAAAANSUhEUgAAABgAAAAYEAYAAACw5+G7AAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QAAAAAAAD5Q7t/AAAACXBIWXMAAABgAAAAYADwa0LPAAAAB3RJTUUH5QkYFxkiRelgmQAABTBJREFUWMPVWE1MU1kUvu/ep9DOq4ArBSFgERwzyShogVGBgJJHCy1QEy3GxkzKRJAfFzPMVhfO7BxAolWqIqBoMskEFRN1YzX+bEw0KrRoRAkUZ0QT+kqE0r4zi3KoMDblb8zM3dykOffc77x7zne+U44sasXHUzM1U7PBAJGkltTqdNwkd5+7n5jI2bkWriUhAS0hByqgYmAAIiAbsvv7OR9pJs3d3bJNtsm2rq6A1eDg4vCEXXFx3EVWzIpbWpiCF5jC76eUMcYAlimXC8sFWU5JSUlRqwGys7dty8wM7uu067TqAllGOzzHBD6aj/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FOet3BLfyMw+tNISj0cB8DC/debM4E9myJKsMlHh4n3xuMtlWqWz0+GI3Oz79mnaZBoZGWGOiwPOHdFD8dMAgHbi6h9/KBJrJR1KyIqFiopgMFJ4SYkp0OVyOCI9Lnukvd1sROqkkZdfNnliobvbrBedABajs6dHkTwusVbSYbQ9IQPnwAFNS00FyspsNo8H6OgwBdpswK02p3wvdbJOeKINMtGxZcu1a9nZgOgE+ICltbs7Mgs9u/2vbdsA1KCrtratzW4/csSchIODNxPLRJbBNlnE4mltHR3NzASWLg0Gn34aAOCgbqcTWLawes+qVRHuMy8SksdXfhDMy3ndMCRUjcedCo/Fc/q0+burP7j6TM4qw6Ak8uBLnw8wysh1QxYSmDcguUj4TrFuTRqLtdEaudsQ4QVv+Le88Buz/3fWLXaArxjD+LWyElhOy+nChVs0IPgvb+ezs65O8njuPN/mBQPMbW2h0IMPRt8jE4XwiFVy5/o2zz/DfPaQsSO+mxmgX5C0fj3wWkON4XaPX38bEpp3N5UWFVEiHjOe27VLYq2kw7VrzZBVWGhmFRn50SDHYU9PKBR5pTx+XNfvvRcQq4w98VsLn0ADgp2nnM7UVMnjEmslHUrIkqwiI19Wn7dxS/xXzIM/GeGE+wAjzFXjLvWPU39j45jHb7TKXWjgpjfz778U/L1eWFwM0CLjw6IiAC/Sn1lZZMc0vB3xt0oAF/HJ0JAMIMA4So1uN0DzVHdXF7Ds5LKTt5ODb8Q/ND+4MoSuQQsAAAAldEVYdGRhdGU6Y3JlYXRlADIwMjEtMDktMjRUMjM6MjQ6NTIrMDA6MDAXpu06AAAAJXRFWHRkYXRlOm1vZGlmeQAyMDIxLTA5LTI0VDIzOjI0OjUyKzAwOjAwZvtVhgAAAABJRU5ErkJggg==
""")
)
self.style.element_create("tab_btn_close", "image", "img1",
#("active", "pressed", "!disabled", "img2"),
("active", "!disabled", "img3"), border=15, sticky=''
)
self.style.layout("ScrollableNotebook", [("ScrollableNotebook.client", {"sticky": "nswe"})])
self.style.layout("ScrollableNotebook.Tab", [
("ScrollableNotebook.tab", {
"sticky": "nswe",
"children": [
("ScrollableNotebook.padding", {
"side": "top",
"sticky": "nswe",
"children": [
("ScrollableNotebook.focus", {
"side": "top",
"sticky": "nswe",
"children": [
("ScrollableNotebook.label", {"side": "left", "sticky": 'nsew'}),
("ScrollableNotebook.tab_btn_close", {"side": "left", "sticky": 'nsew'}),
]
})
]
})
]
})
])
self.style.configure('ScrollableNotebook',
background='#082032',
)
self.style.configure("ScrollableNotebook.Tab",
background='#FDD2BF',
foreground='#012443',
padding=[2, 2],
anchor="center",
justify="center",
font=('Sans-Serif', 12, font.BOLD)
)
self.style.map('ScrollableNotebook.Tab', background = [("selected", "#B61919"),
("active", "#FF6B6B")],
foreground = [("selected", "#ffffff"),
("active", "#012443")]
)
def _wheelscroll(self, event):
# if event.delta > 0:
# Thread(target=self._leftSlide, daemon=True).start()
# else:
# Thread(target=self._rightSlide, daemon=True).start()
global count
# # respond to Linux or Windows wheel event
# if event.num == 5 or event.delta == -120:
# count -= 1
# Thread(target=self._leftSlide, daemon=True).start()
# #self._rightSlide()
# if event.num == 4 or event.delta == 120:
# count += 1
# Thread(target=self._rightSlide, daemon=True).start()
# #self._leftSlide()
print(count)
def _bottomMenu(self,event):
self.text_font = tkFont.Font(family='Consolas', size=13)
tabListMenu = Menu(self, tearoff = 0)
for tab in self.notebookTab.tabs():
tabListMenu.add_command(label=self.notebookTab.tab(tab, option="text"),
command= lambda temp=tab: self.select(temp),
background='#ccffff',
foreground='blue',
font=self.text_font,
activebackground='#004c99',
activeforeground='white')
tabListMenu.entryconfig('WorkSpace ',
accelerator="ALT+W",
image=self.WorkSpac_icon,
compound='left',
label=' WorkSpace')
try:
tabListMenu.tk_popup(event.x_root, event.y_root)
except:
self.bottomTab.configure(background='#DF2E2E',
foreground='#F6D167')
def _bottomMenu_novo(self,event):
self.text_font = tkFont.Font(family='Consolas', size=13)
self.tabListMenu = Menu(self, tearoff = 0)
#for tab in self.notebookTab.tabs():
self.tabListMenu.add_command(
label=" Extraciones",
#accelerator='Ctrl+F',
command=self._abrir_issuesEXT,
background='#ccffff', foreground='black',
activebackground='#004c99',activeforeground='white',
font=self.text_font,
)
self.tabListMenu.add_command(
label=" Desviaciones",
#accelerator='Ctrl+F',
command=self._abrir_issuesEXT,
background='#ccffff', foreground='black',
activebackground='#004c99',activeforeground='white',
font=self.text_font,
)
self.tabListMenu.tk_popup(event.x_root, event.y_root)
def _abrir_issuesEXT(self):
from Example import Aplicacion
Aplicacion.abrir_ISSETX(Aplicacion)
def _tabChanger(self,event):
if event.state == 0:
self._resetSlide(event=None)
try:
self.notebookContent.select(self.notebookTab.index("current"))
except: pass
def _rightSlide(self):
global release
release = False
self.rightArrow.configure(foreground='#DF2E2E')
while not release:
time.sleep(0.01)
if self.notebookTab.winfo_width()>self.notebookContent.winfo_width()-self.menuSpace:
if (self.notebookContent.winfo_width()-(self.notebookTab.winfo_width()+self.notebookTab.winfo_x()))<=self.menuSpace+5:
self.xLocation-=20
self.notebookTab.place(x=self.xLocation,y=0)
else:
self._release_callback(e=None)
def _leftSlide(self):
global release
release = False
self.leftArrow.configure(foreground='#DF2E2E')
while not release:
time.sleep(0.01)
if not self.notebookTab.winfo_x()== 0:
self.xLocation+=20
self.notebookTab.place(x=self.xLocation,y=0)
else:
self._release_callback(e=None)
def _resetSlide(self, event):
self.notebookTab.place(x=0,y=0)
self.xLocation = 0
def add(self,frame,**kwargs):
named = kwargs['text']
if len(self.notebookTab.winfo_children())!=0:
self.notebookContent.add(frame, text=named,state="hidden")
else:
self.notebookContent.add(frame, text=named,state="hidden")
self.notebookTab.add(ttk.Frame(self.notebookTab),**kwargs)
id_tab = self.tabs()[-1]
self.notebookTab.select(id_tab)
def forget(self,tab_id):
#self.notebookContent.forget(self.__ContentTabID(tab_id))
self.notebookTab.forget(tab_id)
def hide(self,tab_id):
#self.notebookContent.hide(self.__ContentTabID(tab_id))
self.notebookTab.hide(tab_id)
def identify(self,x, y):
return self.notebookTab.identify(x,y)
def index(self,tab_id):
return self.notebookTab.index(tab_id)
#return self.notebookTab.index(self.notebookTab.select('current'))
def __ContentTabID(self,tab_id):
return self.notebookContent.tabs()[self.notebookTab.tabs().index(tab_id)]
def insert(self,pos,frame, **kwargs):
#self.notebookContent.insert(pos,frame, **kwargs)
self.notebookTab.insert(pos,frame,**kwargs)
def select(self,tab_id):
self.notebookTab.select(tab_id)
if tab_id == '.!scrollablenotebook.!notebook2.!frame':
self._resetSlide(event=None)
self._release_callback(e=None)
elif tab_id == '.!scrollablenotebook.!notebook2.!frame2' or tab_id == '.!scrollablenotebook.!notebook2.!frame3' or tab_id == '.!scrollablenotebook.!notebook2.!frame4':
Thread(target=self._leftSlide, daemon=True).start()
self.rightArrow.configure(foreground='#297F87')
else:
Thread(target=self._rightSlide, daemon=True).start()
self.leftArrow.configure(foreground='#297F87')
def tab(self,tab_id, option=None, **kwargs):
kwargs_Content = kwargs.copy()
kwargs_Content["text"] = "" # important
#self.notebookContent.tab(self.__ContentTabID(tab_id), option=None, **kwargs_Content)
return self.notebookTab.tab(tab_id, option=None, **kwargs)
def tabs(self):
#return self.notebookContent.tabs()
return self.notebookTab.tabs()
def enable_traversal(self):
self.notebookContent.enable_traversal()
self.notebookTab.enable_traversal()
Hay muchos comentarios porque voy haciendo pruebas, sorry.