简体   繁体   中英

Python, Tkinter: Scrollbar on canvas with entry widgets

EDIT: Sorry, I should've searched more before I asked, because I've found a solution to my problem now. Link: Adding a scrollbar to a group of widgets in Tkinter

I'm trying to add a canvas to a Tkinter GUI, where entry widgets with text from a file is added to a grid. The amount of text in the file tends to be a big, so I need to add a scrollbar.

The entry widgets is added to the canvas the way they're supposed to, and I've independently tested the scrollbar, and it works fine - except when the entry widgets are added. When I do that, the scrollbar disappears.

Here is the code for the canvas and scrollbar:

    groupfr = Frame(self.labelll, width = 200, height = 1000)
    groupfr.pack_propagate(0)
    groupfr.pack(fill='both', side='left')

    canvasgroup = Pmw.Group(groupfr, tag_text= 'canvasgroup')   
    canvasgroup.pack(fill = 'both', expand = 1, padx = 1, pady = 1)

    self.canvas = Tkinter.Canvas(canvasgroup.interior(), width = 200,
                                 height = 1000, bg = 'white')

    self.canvas.config(scrollregion=(0,0,20,1000))
    self.sbar = Tkinter.Scrollbar(self.canvas)
    self.sbar.config(command=self.canvas.yview)

    self.canvas.config(yscrollcommand=self.sbar.set)

    self.sbar.pack(side='right', fill='y')

    self.canvas.pack_propagate(0)
    self.canvas.pack(side = 'left', fill = 'y', expand = 'no')

My apologies for the terrible code. This, however, is where the real problems begin:

for items in linematch:
    items = items.split('=')
    list_of_lists.append(items)

    columncount = 0
    rowcount = 0

    for items in list_of_lists:
        rowcount = rowcount + 1
        columncount = 0

        for idx, (words) in enumerate(zip(items)):
            self.canvas.pack_propagate(0)
            colcnt = colcnt + 1
            idx = Tkinter.Entry(self.canvas)
            idx.grid(row=rowcount, column=columncount)
            idx.insert(0, words)

The code takes the file input and makes each line into a list of two items, which is itself appended to another list. This list of lists is used put into entry widgets like this, with each word (and translation) in their own entry widget:

word translation
word translation

etc. (Don't know how important this info is.)

Does anyone get why the scrollbar disappears when the entry widgets is added?

Thanks

You must use the canvas create_window method to add widgets to the canvas. If you use pack or grid they won't be part of the canvas and thus won't be affected by the scrollbars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM