Python – Adjust scrollbar height in Tkinter

pythonscrollbartkinter

I have created a scrollbar in Tkinter and it's working fine, but the size of the Scroll Button is not being scaled correctly (normally it's adjusted to the size of the scrollable area).

I'm placing all my widgets with a .pack(), therefore the .grid sticky configuration is something I would like to avoid.

My question is: Which part of the scrollbar configuration is responsible for scaling the height?

The code example:

    master = Tk()
    FrameBIG = Frame(master)
    Main = Canvas(FrameBIG,height = 1200,width =1500,scrollregion=Main.bbox("all"))
    scroll = Scrollbar(FrameBIG ,orient="vertical", command=Main.yview)
    scroll.pack(side="right", fill="y")
    Main.pack(side = BOTTOM, anchor = NW,fill="x")
    FrameBIG.pack(anchor = W, fill = "x")

Best Answer

The code

Main = Canvas(FrameBIG,height=1200,width=1500,scrollregion=Main.bbox("all"))

is wrong because Main does not exists yet. It should be

Main = Canvas(FrameBIG,background="blue", height = 500,width =500)
Main.configure(scrollregion=Main.bbox("all"))

But it is meaningless because Main canvas was created right now and is empty (so the bbox method returns None)

When you created the scrollbar with

scroll = Scrollbar(FrameBIG ,orient="vertical", command=Main.yview)

you forgot to complete the two step contract between scroll and Main, so you have to add the line below (just after the creation of scroll)

Main.configure(yscrollcommand=scroll.set)

Now the code is like this

from tkinter import *

master = Tk()
FrameBIG = Frame(master)

Main = Canvas(FrameBIG,background="blue", height = 500,width =500)
Main.configure(scrollregion=Main.bbox("all"))

scroll = Scrollbar(FrameBIG ,orient="vertical", command=Main.yview)
Main.configure(yscrollcommand=scroll.set)

scroll.pack(side="right", fill="y")
Main.pack(side = BOTTOM, anchor = NW,fill="x")
FrameBIG.pack(anchor = W, fill = "x")

master.mainloop()

Now you can notice that the scroll bar does not have the button. Its because the Main canvas is empty. Let's add something to the Main canvas

FrameBIG.pack(anchor = W, fill = "x")

# creates a diagonal from coordinates (0,0) to (500,1000)
Main.create_line(0, 0, 500, 1000)

master.mainloop()

Now the line is there but the scroll button is not there yet, why? Because you have to update the scrollregion of the Main canvas. So let's do it with

FrameBIG.pack(anchor = W, fill = "x")

Main.create_line(0, 0, 500, 1000)
Main.configure(scrollregion=Main.bbox("all"))

master.mainloop()

Now it is working properly. Here the complete code.

from tkinter import *

master = Tk()
FrameBIG = Frame(master)

Main = Canvas(FrameBIG,background="blue", height = 500,width =500)
Main.configure(scrollregion=Main.bbox("all"))

scroll = Scrollbar(FrameBIG ,orient="vertical", command=Main.yview)
Main.configure(yscrollcommand=scroll.set)

scroll.pack(side="right", fill="y")
Main.pack(side = BOTTOM, anchor = NW,fill="x")
FrameBIG.pack(anchor = W, fill = "x")

Main.create_line(0, 0, 500, 1000)
Main.configure(scrollregion=Main.bbox("all"))

master.mainloop()

In the next question, post a question with a complete working code that shows up you problem. You will get faster and better answers, ok? Have a nice day.

Related Topic