您有两个问题:
-
您应该创建的第二个滚动条
b
而不是
a
-
你忘了打电话
.pack()
在第二个画布和滚动条上
以下是修改后的代码:
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()
建议创建
可滚动框架
类以减少冗余代码。