-1

I'm attempting to add a scrollbar widget to the Canvas in my program. The program is a basically a table made by adding a Canvas to the master and then adding Frames to the Canvas using .grid(). I've included the relevant portions of code. I'm not sure what it is that I'm doing incorrectly.

from Tkinter import *    
master = Tk()
canvas = Canvas(master)
title_frame = Frame(canvas, bd=2, relief=RIDGE)
...
canvas.pack()
vbar = Scrollbar(master, orient=VERTICAL)
vbar.config(command=canvas.yview)
vbar.pack(side=RIGHT, fill=Y)
canvas.config(yscrollcommand=vbar.set)
master.mainloop()

This is what my program is producing.

jnkjn
  • 67
  • 1
  • 1
  • 7
  • I think you wanted to include the line `canvas.config(yscrollcommand=ybar.set)` instead of `canvas.config(xscrollcommand=hbar.set)` since the previous lines are about `vbar` and not `hbar`. – j_4321 Jul 18 '16 at 19:19
  • @j_4321 I accidentally typed that in when I was posting my question. – jnkjn Jul 18 '16 at 19:52
  • The code you posted does not produce the output you say it does. Also, the canvas can't scroll widgets added using `.grid()`. It can only scroll objectr create on the canvas itself (eg: with `create_window`). See http://stackoverflow.com/a/3092341/7432 – Bryan Oakley Jul 18 '16 at 20:36
  • This question is probably a duplicate. Look at some of the 'Related' questions on the right sidebar. – Terry Jan Reedy Jul 19 '16 at 00:08

1 Answers1

0

I just added side=LEFT when I packed the canvas and the scrollbar is now on the right of the canvas instead of below. It is because the default side for pack is TOP.

from Tkinter import *    
master = Tk()
canvas = Canvas(master)
canvas.pack(side=LEFT)
vbar = Scrollbar(master, orient=VERTICAL)
vbar.config(command=canvas.yview)
vbar.pack(side=RIGHT, fill=Y)
canvas.config(yscrollcommand=vbar.set)
master.mainloop()

scrollbar

Edit: I think I understand your problem now: I tried to display both horizontal and vertical scrollbars but I didn't manage to do it with the pack layout. I suggest you to use grid instead:

from Tkinter import *    
master = Tk()
canvas = Canvas(master)
canvas.grid(row=0,column=0)
hbar = Scrollbar(master, orient=HORIZONTAL)
hbar.config(command=canvas.xview)
hbar.grid(row=1, column=0, sticky=E+W)
vbar = Scrollbar(master, orient=VERTICAL)
vbar.config(command=canvas.yview)
vbar.grid(row=0,column=1,sticky=N+S)

canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
master.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
  • I made the change you suggested and the program still does not display a Scrollbar. Any other suggestions or things I should try? – jnkjn Jul 18 '16 at 19:55
  • I have edited my answer to show you a solution using grid instead of pack. – j_4321 Jul 18 '16 at 20:10
  • Thanks, I'm away from my computer now, but when I get back to it I'll try it out. I really appreciate the help you've given me. – jnkjn Jul 18 '16 at 20:27