-1

Basically, I am trying to add a scrollbar to a window containing widgets. I am able to successfully add a scrollbar to a Listbox widget and after inserting stuff, the program works just the way I want it to. But, I face a problem when I place widgets into the Listbox. The scrollbar appears but it seems to be disabled.

 from tkinter import *

 root = Tk()
 root.geometry("640x480")
 root.resizable(0,0)
 myscrollbar = Scrollbar(root)
 myscrollbar.pack(side=RIGHT, fill=Y)

 mylist = Listbox(root, width=640, height=480, yscrollcommand=myscrollbar.set)
 mylist.pack(fill=BOTH, expand=True)





 for x in range(1, 101):
    mylist.insert(END, Label(mylist, text="Label: "+str(x)).grid(row=x, column=0))

 myscrollbar.config(command = mylist.yview)

 root.mainloop()

Any way to fix this code?

S.Ahmed
  • 191
  • 1
  • 4
  • 12
  • The `Listbox` is not the adapted widget to do that kind of thing, it is not meant to support child widgets. You should use a `Text` or `Canvas` widget (see the answer to https://stackoverflow.com/questions/3085696/adding-a-scrollbar-to-a-group-of-widgets-in-tkinter). – j_4321 Aug 29 '17 at 07:44
  • Please separate your question. Keep each question post to one simple and easy to answer question, if you have any more difficulties with your code then please raise another ticket. – Ethan Field Aug 29 '17 at 08:31
  • Your title mentions scrolling in a listbox, but then body of the question is asking something else. What is the real point of your question? If you're asking about the solution using a canvas, please rewrite your question to remove all of the code and text related to the listbox. – Bryan Oakley Aug 29 '17 at 11:45

1 Answers1

0
from tkinter import *

def myScrollcmd(event):
    mycanvas.config(scrollregion=mycanvas.bbox('all'))    

root = Tk()
mycanvas = Canvas(root)
mycanvas.pack(fill=BOTH, expand=True)
myFrame = Frame(mycanvas)
mycanvas.create_window((0, 0), window=myFrame, anchor=NW)
myScrollbar = Scrollbar(mycanvas, orient=VERTICAL, command=mycanvas.yview)
myScrollbar.pack(side=RIGHT, fill=Y)
mycanvas.config(yscrollcommand=myScrollbar.set)
mycanvas.bind("<Configure>", myScrollcmd)

for x in range(100):
    Label(myFrame, text="Text "+str(x)).pack()

root.mainloop()

This works. However, there is one problem that might not be a major one. The problem is, when my cursor is on the canvas, and I'm moving my mouse wheel, the scrollbar doesn't budge. However the scroll bar moves along with my mouse wheel when my cursor is on top of the scroll bar. I can drag the scroll box to scroll up and down and use the scroll buttons to scroll up and down but the mouse wheel only works when my cursor is hovering over the scrollbar widget.

S.Ahmed
  • 191
  • 1
  • 4
  • 12