【问题标题】:Scrollbar displaying but not scrolling the dynamically created combobox widgets滚动条显示但不滚动动态创建的组合框小部件
【发布时间】:2026-02-01 07:15:01
【问题描述】:

我在标签框架上创建了一个画布、滚动条。使用按钮,在画布上动态创建组合框小部件。但滚动条滚动动态创建。

  from tkinter import *      
  from tkinter import ttk

  root = Tk()      
  root.geometry("1366x705+0+0")


  ExtRole_Dest_LF = ttk.LabelFrame(root, text='ExternalRoles', width =600)
  ExtRole_Dest_LF.place(relx=0.225, rely=0.113, relheight=0.376, relwidth=0.264)

  canvas=Canvas(ExtRole_Dest_LF,bg='#FFFFFF', height = 110, width = 335, scrollregion=(0,0,500,800))
  canvas.grid(column = 0, row = 0, sticky = 'news')
  canvas.grid_propagate(0)
  canvas.config(scrollregion=canvas.bbox("all"))


  vbar=Scrollbar(ExtRole_Dest_LF,orient=VERTICAL, command=canvas.yview)
  vbar.grid(row = 0, column = 1,  sticky='ns')
  canvas.configure(yscrollcommand=vbar.set)

  global System_Dest_row
  System_Dest_row = 1
  def fn_SystemDest():
      global System_Dest_row
      System_Dest_col = 0
      System_Dest_cb = ttk.Combobox(canvas, values=['a','s','d','g'], width=15)
      System_Dest_cb.grid(row=System_Dest_row, column=System_Dest_col, padx=10, pady = 5)
      deletebutton = Button(canvas, text="X")
      deletebutton.grid(row=System_Dest_row, column=System_Dest_col + 1, padx=10, pady = 5)
      System_Dest_row += 1

  AddButton = Button(root, text = 'Add', command =fn_SystemDest )
  AddButton.grid(column = 3,row = 3)

  root.mainloop()

我可以制作滚动条来滚动动态组合框小部件

我能否以任何其他方式提供解决方案来滚动 LabelFrame/Frame 上的组合框小部件

【问题讨论】:

    标签: python tkinter tkinter-canvas


    【解决方案1】:

    您无法使用grid 滚动添加到画布的内容。画布只能使用create_ 函数之一滚动添加到其中的项目。

    【讨论】:

    • 我可以在没有画布的情况下滚动浏览 Lable Frame 上的组合框小部件。我的意思是滚动的任何替代解决方案