Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在tkinter中的现有元素中添加属性_Python_Tkinter - Fatal编程技术网

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

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

我正在尝试在文本小部件中创建滚动条。我在谷歌上搜索,看到了现有的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
。这意味着滚动条消失了。试一试

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')