Search code examples
pythontkintertkinter-canvasttk

My canvas doesnt let me scroll python


I was creating a youtube manager for my personal use, and it was going decently well, until I hit a brick wall with the UI.

Here's the code:

class Application(ttk.Frame):
def __init__(self, master=None):
    super().__init__(master)
    self.pack()
    self.create_widgets()

def create_widgets(self):
    nb = ttk.Notebook(self)

    page1 = ttk.Frame(nb, width= 300)
    #page1 = ttk.Frame(self, width= 300)

    nb.add(page1, text='One')
    #page1.grid()

    nb.grid()

    frames = {}
    labels = {}
    lk_btns = {}
    cmt_btns = {}
    mk_wch_btns = {}
    dwld_btns = {}

    i = 0

    scrollbar = ttk.Scrollbar(page1)
    #scrollbar.grid(row = 0,column = 1,sticky = "ns")
    scrollbar.pack(side = "right",fill = "y")

    listbox = tk.Canvas(page1,yscrollcommand = scrollbar.set,)

    #listbox.grid(row = 0,column = 0,sticky = "nsew")
    listbox.pack(side = "left",fill = "both")

    for v in test.getLatestVids():
        frm = ttk.Frame(listbox)
        frm.grid(row=i, column=1,sticky = "E")

        lb = ttk.Label(frm, text=convert65536(v["snippet"]["title"]))
        lb.grid(row = 0,rowspan = 4,column=0,sticky = "E")
        labels[i] = lb

        download = ttk.Button(frm, text="Download")
        download.grid(row = 1,column=1,sticky = "W")
        dwld_btns[i] = download

        mwatched = ttk.Button(frm, text="Mark Watched")
        mwatched.grid(row = 2,column=1,sticky = "W")
        mk_wch_btns[i] = mwatched

        like = ttk.Button(frm, text="Like")
        like.grid(row = 1,column=2,sticky = "W")
        lk_btns[i] = like

        comment = ttk.Button(frm, text="Comment")
        comment.grid(row = 2,column=2,sticky = "W")
        cmt_btns[i] = comment

        frames[i] = frm
        i += 1

    scrollbar.config(command=listbox.yview)
    listbox.configure(scrollregion=(-400, -400, 400, 400))

    #self.quit = tk.Button(self, text="QUIT", fg="red",
    #                      command=root.destroy)

root = tk.Tk()
app = Application(master=root)
app.mainloop()

PS1: The "convert65536" method is a method for handling the emojis in youtube titles. I found it here.

PS2: The "test.getLatestVids" method is an external method I made that returns a list of dictionaries.

The problem here is that the code thinks there is a lot of space, and does'nt use the scrollbar instead. I tried to make this work by limiting the size of "page1", but I failed to do so.

Here are some screenshots:

In fullscreen

Seeing the entire list thanks to multiple desktops


Solution

  • Items added to a canvas with pack, place or grid will not scroll. The only things that will scroll on a canvas are windows added with create_window.

    The most common solution is to add one frame to the canvas, and then pack, place, or grid widgets inside the frame. See http://stackoverflow.com/a/3092341/7432.

    If you're creating a vertical list of text and widgets, an even simpler solution is to use a text widget, which allows you to embed widgets with the window_create method.