scroll bar tkinter not scrolling

11,661

Your scrollbar works fine. However, you've defined the scrollregion exactly the same as the size of the canvas. Therefore there is nothing to scroll to. Try

canvas=Canvas(frame,bg='#FFFFFF',width=500,height=500,scrollregion=(0,0,500,800))

And you will see that you can scroll down 300 pixels.

Full working example code:

app = Tk()

frame=Frame(app,width=500,height=500)
frame.grid(row=0,column=0)
canvas=Canvas(frame,bg='#FFFFFF',width=500,height=500,scrollregion=(0,0,500,800))

vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(yscrollcommand=vbar.set)
canvas.pack()

canvas.create_rectangle((200,300,300,600))

app.mainloop()

From your comment, I get the impression that you are using grid to place widgets on your Canvas. You should not do that, a Canvas is not a Frame in which you can grid widgets. You can create shapes on a Canvas or create a window that contains widgets. If you are trying to make a grid of widgets scrollable, you should place the Frame on the Canvas, not the other way around. See this answer for a great example of making a grid of widgets scrollable.

Share:
11,661

Related videos on Youtube

Joshua Jones
Author by

Joshua Jones

Updated on June 04, 2022

Comments

  • Joshua Jones
    Joshua Jones almost 2 years
    #declare gui object and classes
    app = Tk() #creates instance of Tk()
    app.title('Check sort DCA') # sets title of gui
    #---------------------------------------
    def keepSuggested(): #button press actions 
        es.JournalOut('test2')
    def UseNew():
        es.JournalOut('test1')
    #------------------------------
    frame=Frame(app,width=500,height=500)
    frame.grid(row=0,column=0)
    canvas=Canvas(frame,bg='#FFFFFF',width=500,height=500,scrollregion=(0,0,500,500))
    hbar=Scrollbar(frame,orient=HORIZONTAL)
    hbar.pack(side=BOTTOM,fill=X)
    hbar.config(command=canvas.xview)
    vbar=Scrollbar(frame,orient=VERTICAL)
    vbar.pack(side=RIGHT,fill=Y)
    vbar.config(command=canvas.yview)
    canvas.config(width=500,height=500)
    canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
    canvas.pack(expand=True,fill=BOTH)
    
    
    spacer1 = Label(canvas, text='|')
    spacer2 = Label(canvas, text='|')
    spacer3 = Label(canvas, text='|')
    spacer4 = Label(canvas, text='|')
    spacer5 = Label(canvas, text='|')
    
    Chan_Num = Label(canvas,text='Channel Number')
    Chan_Name = Label(canvas, text='Channel Name')
    NewChan_Num = Label(canvas, text='New Channel Number')
    Set_Title = Label(canvas, text='Set New')
    std_Num=Label(canvas, text='Standard Channel Number')
    std_Name = Label(canvas, text='Standard Channel Name')
    
    Chan_Num.grid(row=0, column=0)
    spacer1.grid(row=0, column=1)
    Chan_Name.grid(row=0, column=2)
    spacer2.grid(row=0, column=3)
    NewChan_Num.grid(row=0, column=4)
    spacer3.grid(row=0, column=5)
    Set_Title.grid(row=0, column=6)
    spacer4.grid(row=0,column=7)
    std_Num.grid(row=0,column=8)
    spacer5.grid(row=0,column=9)
    std_Name.grid(row=0,column=10)
    
    
    
    n=0
    i = 0 # loops through all channel numbers to get print table value.
    while i < nchan:  # prints out all present channels with index and channel number and title #populates tables
        ch_name = tsin.GetChanTitle(i)
        ch_num = tsin.GetChanNumber(i)
    
    
        ch_name_list = Label(canvas, text=ch_name )
        ch_num_list = Label(canvas, text=str(ch_num))
    
    
    
        ch_name_list.grid(row=i + 1, column=2)
        ch_num_list.grid(row=i + 1, column=0)
        UserInput=StringVar()
        EntryBox= Entry(canvas, textvariable = UserInput)
        EntryBox.grid(row=i+1,column=4 )
    
    
    
    
        i = i + 1
    j=0
    while j< len(CorrectChannels):
        stdList= CorrectChannels[j]
        stdListNum = j
        std_ch_num= Label(canvas,text=stdListNum+1) 
        std_ch_name = Label(canvas,text=stdList)
        std_ch_name.grid(row=j+1, column=10)
        std_ch_num.grid(row=j+1, column=8)
        j=j+1 
    #build gui elements
    Buttonnew = Button(canvas, text='Set Channels', bg='blue', fg='white',command=UseNew)
    Buttonnew.grid(row=1, column=6)
    Buttonkeep = Button(canvas, text='keep channels', bg='blue', fg='white', command=keepSuggested)
    Buttonkeep.grid(row=2, column=6)
    
    
    
    
    
    app.mainloop()
    

    When I run my tkinter code python code I get a scroll bar with no scroll ability, all of my widgets are in canvas and display properly however scroll is needed to scroll down to see them all, this code is producing a scroll bar however it isn't working.

  • Joshua Jones
    Joshua Jones about 9 years
    Hi thanks, this makes sense, however it doesn't fix my problem, it makes the scroll bar itself 800px, however the grid widgets I have on my canvas just overflow the page height imgur.com/J4nwG2W <-- see
  • fhdrsdg
    fhdrsdg about 9 years
    Using the exact code I just posted, that's not what happens for me. But are you saying you use grid to place widgets on the canvas?
  • Joshua Jones
    Joshua Jones about 9 years
    yes, does this not work properly? I will look at that result, I have seen it previously, must have escaped me that it was useful, thanks ever so much for your help, should I put my grid on my frame and then my frame on my canvas? Just replaced all my canvas references to frame references and it crashes when I hit run for reference I am developing an application in glyphworks which is part of nCode
  • fhdrsdg
    fhdrsdg about 9 years
    Exactly, you should put your canvas and scrollbar in your main window, your widget grid on your frame and your frame on your canvas using canvas.create_window. Just look at the answer I linked, it does exactly that and is a really great template for a scrollable frame.
  • Joshua Jones
    Joshua Jones about 9 years
    I have got it working now thank you so much for explaining it, I was having a hard time following the code in the other example, cheers.
  • Pramod Solanky
    Pramod Solanky almost 7 years
    That got me on the track. Thanks :)