How do you create a LabelFrame with a scrollbar in

2019-02-19 02:41发布

问题:

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 object descended from Frame. One of the LabelFrame descendants is two columns of corresponding Label and Entry objects.

The problem is that there are a varying number of Label and Entry pairs, and there can be more than fit on the screen. I need a way to make a scrollbar for this LabelFrame so that everything fits on the screen. I've tried various ways of making a Scrollbar object, but nothing seems to work. How can I bind a scrollbar to this frame?

Also, I need to be able to refresh or reload this LabelFrame when the load_message() method is called, but it just redisplays the new pairs on top of the old ones (so when there are less pairs in the new set, the old set is still visible at the bottom). I've tried using grid_forget() but either nothing changes or the whole frame doesn't display. How can I forget this display and then redisplay it?

Here is the code for this class:

class freq_frame(LabelFrame):
    def __init__(self, master = None, text = 'Substitutions'):
        LabelFrame.__init__(self, master, text = text)
        self.grid()
    def load_message(self):
        self.frequency = get_freq(message)
        self.create_widgets()
    def create_widgets(self):
        self.label_list = [Label(self, text = get_label(char, self.frequency[char]), justify = LEFT) for char in self.frequency.keys()]
        self.entry_list = [Entry(self, width = 1) for char in self.frequency.keys()]
        for n in range(len(self.label_list)):
            self.label_list[n].grid(column = 0, row = n)
        for n in range(len(self.entry_list)):
            self.entry_list[n].grid(column = 1, row = n)

If anyone can help with either of these problems, I'd appreciate it.

Also, this question seems like it might be a little thin, but I don't know what to add. Don't hesitate to ask for more information (but be specific).

Thanks!

回答1:

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.