代码之家  ›  专栏  ›  技术社区  ›  Santiago E. 98

选项卡控件不显示组合框。显示中出现问题

  •  0
  • Santiago E. 98  · 技术社区  · 2 年前

    如何显示元素( ComboNew )在选项卡控件中 Example 2 ?

    如果我打开 示例2 ,里面什么都没有,里面是空的。我认为问题与滚动条框架有关,但我无法理解。

    我想显示组合框 组合新建 在选项卡控件内 示例2 (显然包括向下滚动条)。

    我做错了什么?(显然包括向下滚动条)。

    你能解释一下我做错了什么,并给我看完整的代码吗?非常感谢。

    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()
    
    0 回复  |  直到 2 年前
        1
  •  1
  •   acw1668    2 年前

    您有两个问题:

    • 您应该创建的第二个滚动条 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()
    

    建议创建 可滚动框架 类以减少冗余代码。

        2
  •  0
  •   toyota Supra    2 年前

    编辑:我添加了图片。第79行: 更改此项:

    ttk.Scrollbar(a,
    

    到 ttk。滚动条(a,

    和第93行: 更改此项:

    comboNew=ttk.Combobox(scrollable_frame,
    

    comboNew=ttk.Combobox(b,
    

    图片:

    enter image description here