mkHun
mkHun

Reputation: 5927

Add the attribute in existing elements in tkinter

I'm trying to create the scrollbar in the Text widget. I search on google and have seen the existing SO questions but I'm not get the proper example.

mycontainer = Text(root)
scrollbar = Scrollbar(mycontainer)
scrollbar.pack( side = RIGHT, fill=Y )

#here I want to add the attribute of yscrollcommand into the mycontainer

mycontainer = Text(yscrollcommand = scrollbar.set) #Not working 

for line in range(100):
   mycontainer.insert(END, "This is line number " + str(line))


mycontainer.place(x=5, y=40, width=500, height=500)
scrollbar.config( command = mycontainer.yview )

How can I do it properly?

Upvotes: 0

Views: 540

Answers (1)

Aemyl
Aemyl

Reputation: 2194

One problem is that you recreate mycontainer after you create the Scrollbar instance. That means that the scrollbar disappears. Try

mycontainer.config(yscrollcommand=scrollbar.set)

instead. Another (small) problem is that you need to finish your inserts with a linebreak like this:

for line in range(100):
    mycontainer.insert(END, "This is line number " + str(line) + "\n")

The third problem is that the scrollbarslider isn't shown correctly (even not with scrollbar.activate('slider')). I have to say that I couldn't solve the last problem. To see all options for the .config() command, type mycontainer.keys() and scrollbar.keys().

Upvotes: 2

Related Questions