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

k

我正在尝试在“文本”小部件中创建滚动条。我在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 )

我该怎么做呢?

艾美

一个问题是您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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章