-1

I am currently trying to make a scrollable list of entries using tkinter in Python 3. While looking for some documentation, I found this: Adding a scrollbar to a group of widgets in Tkinter. It is really great and works really fine with Labels, but it doesn't seem to work with Entries. You'll find right here my code which creates a 2x25 list of entries through which I would like to be able to scroll:

import tkinter as tk


class MainPage(tk.Frame):
    def __init__(self, racine):
        super().__init__(master=racine)
        self.grid()

        self.entriesCanvas = tk.Canvas(self, borderwidth=0, background="white")
        self.entriesFrame = tk.Frame(self.entriesCanvas, background="white")
        self.scrollbar = tk.Scrollbar(self, command=self.entriesCanvas.yview)
        self.entriesCanvas.configure(yscrollcommand=self.scrollbar.set)

        self.entriesCanvas.grid(column=0, row=2, columnspan=2)
        self.scrollbar.grid(column=3, row=2, sticky='ns')

        # self.entriesFrame.grid()
        self.entriesCanvas.create_window((0, 0), window=self.entriesFrame,
                                         anchor='nw', tags='self.entriesFrame')
        # self.entriesFrame.grid()

        self.entriesCanvas.bind('<Configure>', self.onFrameConfigure)

        self.entries = []
        for i in range(50):
            self.entries.append(tk.Entry(self.entriesFrame, font=('arial', 30)))
            self.entries[i].grid(column=i % 2, row=i//2)

    def onFrameConfigure(self, event):
        self.entriesCanvas.configure(scrollregion=self.entriesCanvas.bbox("all"))


if __name__ == "__main__":
    root = tk.Tk()
    mainPage = MainPage(root)
    root.mainloop()

Notice I commented two lines out. If you "activate" the first line, there will be a scrollbar and one can scroll through the entries, but it is strangely zoomed. On the other hand, if you "activate" the second line, the GUI will be as I would like it to be, but without the possibility to scroll, and it seems to show all entries (even if there are 1000 entries, therefore making a window which is 20 times the size of your screen).

Do you know where my mistake is?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • You have to use `.create_window` therefore using `.entriesFrame.grid()` is wrong. Also remove all `.entriesCanvas.grid_...configure(...`. Read [How to get frame in canvas window to expand to the size of the canvas?](https://stackoverflow.com/questions/29319445) – stovfl Jan 01 '20 at 23:17
  • For the `.entriesCanvas.grid_...configure(...)`, it was part of a random test I was doing and I forgot to delete, so I've just updated the post. However, I don't understand how not to use ´.entriesCanvas.grid()´. Does it mean I should use `.pack()` insted? I'm quite new with tkinter but it seemed to be a bad idea to mix `.pack()` and `.grid()`. – Joachim Favre Jan 01 '20 at 23:41
  • ***"bad idea to mix"***: Yes, will throw an error. Try to rephrase your problem and remove not relevant info like *"put two lines in comment"* – stovfl Jan 01 '20 at 23:47
  • There should literally be no difference between using `Label` widgets and any other widget, including `Entry`. – Bryan Oakley Jan 02 '20 at 02:36
  • You're right, the problem didn't come from the use of Entries instead of Labels, as it came from the size of my Entries (which are quite big as their font is Arial-30). – Joachim Favre Jan 02 '20 at 12:20

1 Answers1

0

Okay, so I found a way to have my program doing what I want. I just changed the method

def onFrameConfigure(self, event):
    self.entriesCanvas.configure(scrollregion=self.entriesCanvas.bbox("all"))

by

def onFrameConfigure(self, event):
    self.entriesCanvas.configure(scrollregion=self.entriesCanvas.bbox("all"), width=self.entriesFrame.winfo_width())

(I basically only added a parameter to ask the canvas to be the width of the frame.) I don't know if it is perfect (as I still use .grid() istead of .pack()), but it works.

Thank you for giving me the source Tkinter: How to get frame in canvas window to expand to the size of the canvas?, it really helped me find where my mistake was.

I truely apologise for my English, I know I make a lot of mistakes.