Home > OS >  Add new TabControls inside another TabControl
Add new TabControls inside another TabControl

Time:06-20

I have a Main TabControl (Cronaca). Inside I have a second TabControl (Incident).

I would like to insert a third new TabControl called "Example" into Incident. The new Tabcontrol "Example" will have to contain elements such as those comboboxes (those that were previously contained in Incident, then remove them from Incident). How can I do?

Can you show me this simple code? I know, I have already plugged a Tabcontrol into another Tabcontrol, but I am currently confused and in the early stages of Pyhon. Help will be useful to me

P.S: OPTIONAL: My code is probably written in an unordered way. If it is possible, can some good person also make order in the staves? (the staves that come before and the staves that come after). Thank you

enter image description here

from tkinter import *
from tkinter import ttk
import tkinter as tk


window=Tk()
window.attributes('-zoomed', True)
window.configure(bg='#f3f2f2')

style = ttk.Style(window)
style.theme_use('clam')


#######################
tabControl = ttk.Notebook(window, style='Custom.TNotebook', width=700, height=320)
  
cronaca = ttk.Notebook(tabControl)
politica = ttk.Notebook(tabControl)
gossip = ttk.Notebook(tabControl)

tabControl.add(cronaca, text ='Cronaca')
tabControl.add(politica, text ='Politica')
tabControl.add(gossip, text ='Gossip')
tabControl.place(x=1, y=1)

#CRONACA
a = ttk.Frame(cronaca)
canvas = tk.Canvas(a)

scrollbar = ttk.Scrollbar(a, orient="vertical", command=canvas.yview)
scrollable_frame = ttk.Frame(canvas, width = 500, height = 500)

scrollable_frame.bind(
    "<Configure>",
    lambda e: canvas.configure(
        scrollregion=canvas.bbox("all")
    )
)


canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)


#Incidente
a.pack()
cronaca.add(a, text="Incidente")

combo1=ttk.Combobox(scrollable_frame, width = 18)
combo1.place(x=20, y=20)
combo1['value'] = ["text1", "text2"] 
          
combo2=ttk.Combobox(scrollable_frame, width = 18)
combo2.place(x=20, y=80)
combo2['value'] = ["text1", "text2"] 

combo3=ttk.Combobox(scrollable_frame, width = 18)
combo3.place(x=20, y=140)
combo3['value'] = ["text1", "text2"] 

combo4=ttk.Combobox(scrollable_frame, width = 18)
combo4.place(x=20, y=200)
combo4['value'] = ["text1", "text2"]

canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")


b = ttk.Frame(cronaca)
cronaca.add(b, text="Microcriminalità")

c = ttk.Frame(cronaca)
cronaca.add(c, text="Criminalità")

d = ttk.Frame(cronaca)
cronaca.add(d, text="Disagi")


#tab 2
c = ttk.Frame(politica)
d = ttk.Frame(politica)


window.mainloop()

CodePudding user response:

You can create another Notebook inside "Incidente" tab and put the scrollable frame inside a "Example" tab inside the new Notebook.

...

#CRONACA
#-- create a Notebook widget inside "cronaca"
incident = ttk.Notebook(cronaca)
#-- add the notebook into the "Incidente" tab
cronaca.add(incident, text="Incidente")

#Incidente
#-- create the scrollable frame inside the "Incidente" tab instead
a = ttk.Frame(incident)
#-- add the scrollable frame into a tab named "Example" inside "Incident" notebook
incident.add(a, text="Example")
...

Below is the updated code:

# avoid using wildcard import
#from tkinter import *
from tkinter import ttk
import tkinter as tk

window = tk.Tk()  # changed from Tk() to tk.Tk()
window.attributes('-zoomed', True)
window.configure(bg='#f3f2f2')

style = ttk.Style(window)
style.theme_use('clam')

#######################
tabControl = ttk.Notebook(window, style='Custom.TNotebook', width=700, height=320)

cronaca = ttk.Notebook(tabControl)
politica = ttk.Notebook(tabControl)
gossip = ttk.Notebook(tabControl)

tabControl.add(cronaca, text ='Cronaca')
tabControl.add(politica, text ='Politica')
tabControl.add(gossip, text ='Gossip')
tabControl.place(x=1, y=1) # suggest to use pack() instead of place()

#CRONACA
#-- create a Notebook widget inside "cronaca"
incident = ttk.Notebook(cronaca)
#-- add the notebook into the "Incidente" tab
cronaca.add(incident, text="Incidente")

#Incidente
#-- create the scrollable frame inside the "Incident" notebook instead
a = ttk.Frame(incident)
#-- add the scrollable frame into a tab named "Example" inside "Incident" notebook
incident.add(a, text="Example")

canvas = tk.Canvas(a)

scrollbar = ttk.Scrollbar(a, orient="vertical", command=canvas.yview)
scrollable_frame = ttk.Frame(canvas, width = 500, height = 500)

scrollable_frame.bind(
    "<Configure>",
    lambda e: canvas.configure(
        scrollregion=canvas.bbox("all")
    )
)

canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)

combo1=ttk.Combobox(scrollable_frame, width = 18)
combo1.place(x=20, y=20)
combo1['value'] = ["text1", "text2"]

combo2=ttk.Combobox(scrollable_frame, width = 18)
combo2.place(x=20, y=80)
combo2['value'] = ["text1", "text2"]

combo3=ttk.Combobox(scrollable_frame, width = 18)
combo3.place(x=20, y=140)
combo3['value'] = ["text1", "text2"]

combo4=ttk.Combobox(scrollable_frame, width = 18)
combo4.place(x=20, y=200)
combo4['value'] = ["text1", "text2"]

canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")

b = ttk.Frame(cronaca)
cronaca.add(b, text="Microcriminalità")

c = ttk.Frame(cronaca)
cronaca.add(c, text="Criminalità")

d = ttk.Frame(cronaca)
cronaca.add(d, text="Disagi")

#tab 2
c = ttk.Frame(politica)
d = ttk.Frame(politica)

window.mainloop()
  • Related