2016-10-28 17 views
0

私は、テキストウィジェットでスクロールバーを作成しようとしています。私はGoogleで検索し、既存のSOの質問を見たが、私は適切な例を得ていない。tkinterの既存の要素に属性を追加します。

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) 

どうすれば適切に行うことができますか?

答えて

2

Scrollbarインスタンスを作成した後で、mycontainerを再作成することが1つ問題です。これは、スクロールバーが消えることを意味します。代わりに

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()と入力してください。

+0

質問表示のようなステートメントの順序を正確に使用したとき、私はもう3番目の問題を抱えませんでした。そして、 'scrollbar.activate( 'slider')' – Aemyl

関連する問題