0

I have added widget after press of a button in the below code. Now I want to add scrollbar to scroll the added widgets but the scrollbar is not working. Can someone please post the working code for the same as I am unable to find the solution.

from Tkinter import *


def create_line ():
    """ """
    global N

    Label (canvas, text= "Color ").grid(row=N, column=0, padx=3)
    OptionMenu (canvas, v, *optionList).grid(row=N, column=1, padx=3)
    Button (canvas, text="+", command=lambda:add_line()).grid(row=N, column=2, padx=3)

def add_line ():
    global N
    N = N +1
    Label (canvas, text= "Color ").grid(row=N, column=0, padx=3)
    OptionMenu (canvas, v, *optionList).grid(row=N, column=1, padx=3)
    canvas.config()


root = Tk()
frame=Frame(root,width=300,height=300)
frame.grid(row=0,column=0)

canvas=Canvas(frame,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
scrollbar = Scrollbar(frame,orient=VERTICAL)
scrollbar.pack(side=RIGHT,fill=Y)
scrollbar.config(command=canvas.yview)
canvas.config(width=300,height=300)
canvas.config(yscrollcommand=scrollbar.set)
canvas.pack(side=LEFT,expand=True,fill=BOTH)



optionList = ("red", "green", "blue")
N = 0
v = StringVar()
v.set(optionList[0])
create_line()
mainloop()
  • 2
    I guess that you need the canvas to scroll a group of widgets, so you should not grid the widgets inside the canvas but use `canvas.create_window` instead. The answer to this [question](https://stackoverflow.com/questions/3085696/adding-a-scrollbar-to-a-group-of-widgets-in-tkinter) should help you. – j_4321 Jan 31 '18 at 07:59
  • **don't mix place,pack,grid** Only one method is permitted. – dsgdfg Jan 31 '18 at 08:13
  • 4
    @dsgdfg The OP is not mixing methods, but chose to use `pack` inside `frame` and `grid` inside `canvas`, which is perfectly permitted. What is not allowed is to use both `grid` and `pack` inside the same parent widget (it raises an error while the OP's code runs without error). – j_4321 Jan 31 '18 at 09:41

0 Answers0