Home > Net >  Problem in displaying three tabs control X, Y, Z. I only display two tabs and not three
Problem in displaying three tabs control X, Y, Z. I only display two tabs and not three

Time:07-02

Functional and executable code of a tabcontrol that has other tabcontrols inside. Inside Tab 1 I have Tabs X, Y, Z. I only see tabs X and Z, but I don't see the Y.

Initially there were tabs X and Y correctly, then I added tab Z but I encountered the problem.

How can I correctly display the X, Y, Z tabs (including their content with the scrollbar)?

What am I doing wrong? I'm sure the problem is very simple, but I can't find it. Can you show me the full code please? Thank you

import tkinter as tk                    
from tkinter import ttk
  
root = tk.Tk()
root.title("Tab Widget")
root.attributes('-zoomed', True)
tabControl = ttk.Notebook(root, style='Custom.TNotebook', width=400, height=220)

#I create Tab1 and Tab2
tab1 = ttk.Notebook(tabControl)
tab2 = ttk.Notebook(tabControl)
  
tabControl.add(tab1, text ='Tab 1')
tabControl.add(tab2, text ='Tab 2')
tabControl.place(x=1, y=1)





#TAB X
x = ttk.Frame(tab1)
canvas = tk.Canvas(x)

### X CONTENT'S ###
#Scrollbar of X
scrollbar = ttk.Scrollbar(x, 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)

#X Element's
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"]


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

tab1.add(x, text="X")
###################################################

#TAB Y
y = ttk.Frame(tab1)
tab1.add(y, text="Y")


### Y CONTENT'S ###
y_content = ttk.Frame(y)
canvas = tk.Canvas(y_content)

#Scrollbar of Y
scrollbar = ttk.Scrollbar(y_content, 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)

#Y Element's
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"]


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


#TAB Z
z = ttk.Frame(tab1)
tab1.add(y, text="Z")


### Z CONTENT'S ###
z_content = ttk.Frame(z)
canvas = tk.Canvas(z_content)

#Scrollbar of Z
scrollbar = ttk.Scrollbar(z_content, 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)

#Z Element's
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"]


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




 
root.mainloop()

CodePudding user response:

You have a typo! You wanted to add the y Frame instead of z.

#TAB Z
z = ttk.Frame(tab1)
tab1.add(z, text="Z")  # Here you should add "z" variable instead of "y"

I recommend to add something style configuration for TAB size because the sub-tabs are not visible very well.

For example for type configuration (Put these lines after imports):

style = ttk.Style()
style.theme_settings("default", {"TNotebook.Tab": {"configure": {"padding": [20, 5]}}})

GUI:

GUI

  • Related