2

I built scrollable canvas before, but now I have to fill one of them with buttons, and I don't know why the scrollbar is inactive... maybe the problem is that the canvas contains buttons?

It's strange that it is inactive since the canvas and the scrollbar seem to be correctly connected by vscrollbar.set and cv.yview...

from Tkinter import *

def main():

  root = Tk()
  root.geometry("%dx%d+0+0" % (1800,1000))

  auxframe=Frame(root)
  auxframe.pack(fill=BOTH, expand=YES)

  cv = Canvas(auxframe, scrollregion=(0,0,8000,8000))
  vscrollbar = Scrollbar(auxframe, orient=VERTICAL)
  vscrollbar.pack(fill=Y, side=RIGHT)
  vscrollbar.config(command=cv.yview)
  cv.config(yscrollcommand=vscrollbar.set)
  cv.pack(side=LEFT, fill=BOTH, expand=TRUE)

  memimages=[]    
  for row_index in range(8):
      for col_index in range(8):
          iconimage = PhotoImage(file="test.gif")
          b=Button(cv,image=iconimage)
          memimages.append(iconimage)             
          b.grid(row=row_index, column=col_index, sticky=N+S+E+W)  


  mainloop()

main()

(edit) according to Bryan's answer, I made it work by adding this - note that fcv is not packed.

fcv=Frame(root)
cv.create_window(0, 0, anchor = "nw", window=fcv)

and all the buttons are children of fcv: b=Button(fcv,image=iconimage)

I still dont understand why the documentation (e.g.infohost or tutorialspot) doesnt mention this important fact.

alessandro
  • 3,838
  • 8
  • 40
  • 59

1 Answers1

1

The canvas will not scroll objects added to it via pack, place, or grid. You must use the canvas method create_window.

A common technique is to use create_window to create a single frame, and then you can use pack, place, or grid on widgets that are placed inside the frame. For an example see Adding a scrollbar to a group of widgets in Tkinter

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I tested your suggestion and it works. Unfortunately, the use of create_window leads to a different problem, related to its maximum size, I asked about here https://stackoverflow.com/questions/42830037/tkinter-maximum-canvas-size/42837629#42837629 . It'd be wonderful if you could have a look at it... – alessandro Mar 17 '17 at 10:38