How do you create a LabelFrame with a scrollbar in Tkinter?

前端 未结 1 1981
陌清茗
陌清茗 2021-01-14 16:50

I\'m using Python and Tkinter to create a GUI for a program I\'m writing, and I\'m having a couple of problems.

I have three objects descended from LabelFrame in an

相关标签:
1条回答
  • 2021-01-14 17:37

    Labelframes don't support scrolling. So the short answer to your question is "you can't". It sounds obvious, but if the documentation for a widget doesn't say it supports scrolling, it doesn't support scrolling.

    However, there is a simple solution. First, add a canvas as a child to the labelframe and pack it so that it fills the labelframe. Attach scrollbars to the canvas and add them to the labelframe too. Then embed a frame within the canvas, add your widgets to that inner frame, and then adjust the scrollregion of the canvas to match the size of the frame after you've added all the inner labels and entries.

    It sounds complicated, but it's really very straight-forward.

    As for re-creating the widgets when you call load_message, calling grid_forget only removes them from view, it doesn't actually destroy the widgets. Over time you could potentially end up with hundreds of non-visible widgets which is almost certainly not what you want.

    Instead, you want to first destroy all the existing widgets. That's pretty easy if they all are in the same parent, since you can ask the parent for a list of all its children. Just iterate over that list to delete each child, then add any new children. An even easier solution is to destroy and recreate that inner frame that contains the labels and entries. When you delete a widget, all child widgets get automatically destroyed. So, delete that inner frame, create a new one, and add your labels and entries again.

    0 讨论(0)
提交回复
热议问题