Home > database >  Tabcontrol does not display a combobox. Problem in display
Tabcontrol does not display a combobox. Problem in display

Time:06-20

How can I display an element (ComboNew) in the Tabcontrol Example 2?

If I open Example 2, there is nothing inside, there is emptiness inside. I think the problem is related to the scrollbar frame but I can't understand.

I would like to display the combobox ComboNew inside the Tab Control Example 2 (obviously including the scroll bar to go down).

What am I doing wrong? (obviously including the scroll bar to go down).

Can you explain what I did wrong and show me the complete code? Thank you

enter image description here

from tkinter import ttk
import tkinter as tk

window = 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)

#EXAMPLE
#-- 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")



#EXAMPLE 2
b = ttk.Frame(incident)
incident.add(b, text="Example 2")

canvas = tk.Canvas(b)

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)


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

window.mainloop()

CodePudding user response:

You have two issues:

  • you should create the second scrollbar a child of b instead of a
  • you forget to call .pack() on the second canvas and scrollbar

Below is the modified code:

from tkinter import ttk
import tkinter as tk

window = 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()
tabControl.pack() # 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)

#EXAMPLE
#-- 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")



#EXAMPLE 2
b = ttk.Frame(incident)
incident.add(b, text="Example 2")

canvas = tk.Canvas(b)

#-- create scrollbar as child of 'b' instead of 'a'
scrollbar = ttk.Scrollbar(b, 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)

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

#-- need to call .pack() on the canvas and scrollbar
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")

window.mainloop()

Suggest to create a scrollable frame class to reduce redundant code.

CodePudding user response:

Edit: I added image. In line 79: Change this:

ttk.Scrollbar(a,

to ttk.Scrollbar(a,

and line 93: Change this:

comboNew=ttk.Combobox(scrollable_frame,

to

comboNew=ttk.Combobox(b,

Image:

enter image description here

  • Related