我有一個主 TabControl(克羅納卡)。在里面我有第二個 TabControl(事件)。
我想在 Incident 中插入第三個名為“Example”的新 TabControl。新的 Tabcontrol“示例”將必須包含諸如組合框之類的元素(以前包含在 Incident 中的那些,然后將它們從 Incident 中洗掉)。我能怎么做?
你能告訴我這個簡單的代碼嗎?我知道,我已經將一個 Tabcontrol 插入另一個 Tabcontrol,但我目前很困惑并且處于 Pyhon 的早期階段。幫助對我有用
PS:可選:我的代碼可能是以無序的方式撰寫的。如果可能的話,一些好人也可以在五線譜中進行排序嗎?(之前的五線譜和之后的五線譜)。謝謝

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()
uj5u.com熱心網友回復:
您可以在“Incidente”選項卡中創建另一個Notebook,并將可滾動框架放在新的“示例”選項卡中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")
...
以下是更新后的代碼:
# 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/492987.html
標籤:Python python-3.x tkinter 选项卡控件 tkinter-canvas
