在tkinter中的现有元素中添加属性

时间:2016-10-28 06:22:46

标签: python tkinter

我试图在“文本”小部件中创建滚动条。我在google上搜索并看到了现有的SO问题,但我没有得到正确的例子。

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 )

我该怎么做呢?

1 个答案:

答案 0 :(得分:2)

一个问题是您在创建mycontainer实例后重新创建Scrollbar。这意味着滚动条消失了。尝试

mycontainer.config(yscrollcommand=scrollbar.set)

代替。另一个(小)问题是你需要使用这样的换行符完成插入:

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

第三个问题是滚动条滑块没有正确显示(即使不是scrollbar.activate('slider'))。我不得不说我无法解决最后一个问题。要查看.config()命令的所有选项,请键入mycontainer.keys()scrollbar.keys()