mkHun mkHun - 5 months ago 41
Python Question

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)), y=40, width=500, height=500)
scrollbar.config( command = mycontainer.yview )

How can I do it properly?


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


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().