[Tkinter-discuss] Horizontal scroll canvas

Michael Lange klappnase at web.de
Tue Sep 15 20:13:30 CEST 2009


Hi,

On Tue, 15 Sep 2009 13:48:50 -0400
Bradley Hintze <bradley.h at aggiemail.usu.edu> wrote:

> I am trying to make a scrollable canvas in the horizontal direction
> but I am having a difficult time. I get the whole scroll bar and can
> scroll using the arrows on the ends of the scroll bar but the actual
> bar (the slider bar) takes up the whole scrollbar area as if there is
> nothing to scroll. Thus, the slider bar cannot be used to scroll. (It
> may be easier to understand if you just run the snipit of code) I am
> wondering how I can fix this.
> Here is my code:
> 
> from Tkinter import *
> from tkFileDialog import *
> import os
> class analyze_notes:
> 
>     def __init__(self):
>         string=''
>         for i in range(0,100):string=string+'  '+str(i)
>         self.comp_1=Tk()
>         self.comp_1.title('Try')
>         l1=Label(self.comp_1, text='Try canvas').grid()
>         c1_xscroll=Scrollbar(self.comp_1, orient=HORIZONTAL)
>         c1_xscroll.grid(row=2, sticky=E+W+N)
>         c1=Canvas(self.comp_1, height=150, width=400, relief=SUNKEN,
> xscrollcommand=c1_xscroll.set)
>         c1.grid(row=1)
>         c1_xscroll.config(command=c1.xview)
>         self.topic_label = Label(c1, text=string, font=('courier',10),
> bg='white')
>         c1.create_window(0, 50, window=self.topic_label, anchor=NW)
>         self.comp_1.mainloop()
> 
> r=analyze_notes()
> 

You did not define the Canvases scrollregion; this must be a 4-tuple
(x0, y0, x1, y1) defining the part of the canvas that should be
accessible through the scrollbars. In your case it should be fine to
add a line

    c1.configure(scrollregion=c1.bbox('all'))

to your code right before the call to mainloop() .
Please note that you might have to update the widget's scrollregion
each time the canvases contents are changed.

HTH

Michael


More information about the Tkinter-discuss mailing list