Python 在tkinter中的现有元素中添加属性
我正在尝试在文本小部件中创建滚动条。我在谷歌上搜索,看到了现有的SO问题,但我没有得到正确的例子Python 在tkinter中的现有元素中添加属性,python,tkinter,Python,Tkinter,我正在尝试在文本小部件中创建滚动条。我在谷歌上搜索,看到了现有的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(yscrollc
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
。这意味着滚动条消失了。试一试
mycontainer.config(yscrollcommand=scrollbar.set)
相反。另一个(小)问题是,您需要使用以下换行符完成插入:
for line in range(100):
mycontainer.insert(END, "This is line number " + str(line) + "\n")
第三个问题是scrollbarslider没有正确显示(即使使用
scrollbar.activate('slider')
)。我不得不说我不能解决最后一个问题。要查看.config()
命令的所有选项,请键入mycontainer.keys()
和scrollbar.keys()
一个问题是,在创建scrollbar
实例后重新创建mycontainer
。这意味着滚动条消失了。试一试
mycontainer.config(yscrollcommand=scrollbar.set)
相反。另一个(小)问题是,您需要使用以下换行符完成插入:
for line in range(100):
mycontainer.insert(END, "This is line number " + str(line) + "\n")
第三个问题是scrollbarslider没有正确显示(即使使用
scrollbar.activate('slider')
)。我不得不说我不能解决最后一个问题。要查看.config()
命令的所有选项,请键入mycontainer.keys()
和scrollbar.keys()
Ok,当我完全按照问题所示的语句顺序使用时,我不再遇到第三个问题:),并且我不需要滚动条。激活('slider')
Ok,当我完全按照问题所示的语句顺序使用时,我不再遇到第三个问题:),我也不需要scrollbar.activate('slider')