2

I am trying to create a gui with two canvas and a scrolling bar going through both at the same time, as a test for another project. I have created the root, the two canvas and pinned some labels to each using the grid method, as well as created the scroll bar.

However, when I run the program, the scroll bar moves fines, but the content of the window doesn't change at all, as if the bar wasn't working. I have tried a few solutions by googling my problem, but so far I haven't been able to solve it.

The relevant code is

from tkinter import *
root = Tk()
‪#‎scroll‬
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
firstCanvas = Canvas(root, width=200, height=100)
firstCanvas.pack(side="left", fill="both", expand=True)
secondCanvas = Canvas(root, width=20000, height=10000,scrollregion=(0,0,0,5000),yscrollcommand=scrollbar.set)
secondCanvas.pack(side="left", fill="both", expand=True)
secondCanvas.create_rectangle((200,300,300,6000))
widget = Label(firstCanvas, text='Spam')
widget.pack()

# Lots of widgets so they reach beyond the screen, all in the following format

widgetOne=Label(firstCanvas, text="this is a test")
widgetOne.pack()
widgetTwo=Entry(firstCanvas)
widgetTwo.pack()
widgetThree=Label(secondCanvas, text='Spam')
widgetFour=Entry(secondCanvas)
widgetFour.pack()   

scrollbar.config(command=secondCanvas.yview)
mainloop()
Juan Dougnac
  • 95
  • 10

1 Answers1

3

The canvas will only scroll canvas objects. For widgets, that means widgets created with canvas.create_window(...)

See Adding a scrollbar to a group of widgets in Tkinter

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • It worked fine with the text and label widgets. However, when I add in a few buttons, it throws the error ` "Traceback (most recent call last): File "/home/juanelo/Downloads/prueba2.py", line 978, in populate(frame) File "/home/juanelo/Downloads/prueba2.py", line 966, in populate grabarBTN.grid(row=0,column=6) File "/usr/lib/python3.4/tkinter/__init__.py", line 2060, in grid_configure + self._options(cnf, kw)) _tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack" ` – Juan Dougnac Mar 22 '16 at 15:57
  • @JuanDougnac: read the error message. It's telling you precisely what the problem is. You can't use both grid and pack in the same parent window (Which is what "." represents in that stack trace) – Bryan Oakley Mar 22 '16 at 16:00