0

I wrote this code for a scrollbar:

from tkinter import *

from tkinter.ttk import *


root =  Tk()
root.geometry("500x500")

eventFrame = LabelFrame(root, text = "Upcoming Events", width = 500, height = 500)
eventFrame.pack(pady = 5, padx = 5, anchor = CENTER, fill = BOTH, expand = True)

scrollCanvas = Canvas(eventFrame, width = 50, height = 500)
scrollCanvas.pack(pady = 5, padx = 5)

scrollbar = Scrollbar(scrollCanvas)
scrollbar.pack(side = RIGHT, fill = Y)

eventCanvas = Canvas(scrollCanvas, width = 500, height = 500, yscrollcommand = scrollbar.set)
eventCanvas.pack(pady = 5, padx = 5)

for x in range(100):
    label = LabelFrame(eventCanvas, text = "Title {}".format(x), height = 500, width = 500)
    label.pack(pady = 10, padx = 10, expand = True, fill =  BOTH)

    text = Label(label, text = "This is a test event. no.{}".format(x))
    text.pack(anchor = CENTER, pady = 5, padx = 5, expand = True, fill =  BOTH)

scrollbar.config(command = eventCanvas.yview, scrollregion = eventCanvas.bbox("all"))

root.mainloop()

However, the scrollbar is greyed out. I assume, based on other SO SE answers that I have my bar scrolling the wrong canvas. How would I fix this? I`m not sure how to switch the canvas without messing it up.

doejs
  • 96
  • 10
  • is the scrollbar supposed to scroll what's inside `eventCanvas`, or `scrollCanvas`? It's not entirely clear why you need a canvas inside a canvas, or why you are packing widgets inside a canvas. You have labels inside frames inside a canvas inside a canvas inside a frame. Is all of that layering really necessary? What are you actually trying to accomplish? – Bryan Oakley Aug 26 '16 at 15:32
  • My goal is to create an app that allows users to scan the manifest of a MMORPG and be able to see any event that the game owners have added to the manifest for the future. This allows them to plan game play accordingly. This app is supposed to display these events. I could not get the `Treeview` to work, so I want to do the following: Create a LabelFrame that is scrollable, with smaller LabelFrames inside. However, a LabelFrame is not scrollable, so I tried to add a canvas to the larger LabelFrame, and it all kind of collapsed from there. – doejs Aug 26 '16 at 15:58
  • Why are you using `Canvas` as a container? Plus, `Scrollbar` needs to have `Frame` as a parent, not `Canvas`. There are many redundant parts in your code. And the way you have put widgets inside widgets is complicated. – Parviz Karimli Aug 26 '16 at 16:09
  • You don't need two canvases. You need one canvas that is logically attached to a scrollbar. Inside that canvas is exactly one frame. Inside that frame you can put whatever you want,such as other labelframes. See http://stackoverflow.com/a/3092341/7432 – Bryan Oakley Aug 26 '16 at 16:21
  • @BryanOakley can we talk about it in [this](http://chat.stackoverflow.com/rooms/121956/scrolling-widgets-inside-a-canvas) chat room please? I have some questions. – Parviz Karimli Aug 26 '16 at 17:00
  • @BryanOakley, the example attaches the `Scrollbar` to `root`. I need to put my scrollbar within a `LabelFrame`, so what would the `root` equivalent be to that? A `Canvas` in the `LabelFrame`? – doejs Aug 28 '16 at 16:04
  • You need a frame inside a canvas. You can put that canvas in anything, including the root window, a frame, a labelframe, or anything else. – Bryan Oakley Aug 28 '16 at 17:22
  • These examples are just confusing me more and more. So, so have a `LabelFrame` that scrolls, I need to create the `LabelFrame`, with an embeded `Canvas`, and then put a `Frame` inside the `Canvas`? – doejs Aug 28 '16 at 21:53

0 Answers0