2016-02-28 142 views
2

我正在嘗試使用tkinter小部件製作可滾動的文本。我希望滾動條僅在需要時出現(當我的文本小部件的一部分不可見時)。
我的程序會搜索每一個輸入,如果是這樣的話,如果它是滾動條出現,如果它不是。
它第一次運行良好,但如果我刪除一些文本(所以滾動條消失),然後寫一些,滾動條出現,但沒有滑塊!Python Tkinter Scrollable Text,滑塊不出現

#-*-coding:latin-1-* 

from tkinter import * 


class TextScrollbar(Frame): 
    """ 
     A Text widget which can be scrolled. 
     Text widget with a scrollbar appearing only when you need it 
     (when there is text that you can see) 
     Use self.Text to acccess to your Text widget 
    """ 

    def __init__(self, master=None, cnf={}, **kw): 

     #Creat a Frame which will contain the Text and the Scrollbar widget 
     Frame.__init__(self, master=None, cnf={}, **kw) 

     #Creat Scrollbar widget 
     self.ScrollBar=Scrollbar(self, orient='vertical') 

     #Creat Text widget 
     self.Text=Text(self, cnf={}, **kw) 

     #Link between Text and Scrollbar widgets 
     self.Text.config(yscrollcommand=self.ScrollBar.set) 
     self.ScrollBar.config(command=self.Text.yview)  

     #Distribution of the Text widget in the frame 
     self.Text.pack(side='left', fill=BOTH, expand=1) 


     def _typing(event): 
      """Check whether you need a scrollbar or not""" 
      if Text.ScrollBar.get()==(0.0, 1.0): 
       self.ScrollBar.pack_forget() 
      else: 
       self.ScrollBar.pack(side='right', fill=Y, expand=1) 

     self.Text.bind('<Key>',_typing) 



root=Tk() 
Text=TextScrollbar(root) 
Text.pack(fill=BOTH, expand=1) 

First trial

Second trial

我仍然不知道爲什麼它沒有工作,但通過它的工作原理.grid梅索德更換.pack梅索德,這裏是更新的代碼

#-*-coding:latin-1-* 

from tkinter import * 


class TextScrollbar(Frame): 
    """ 
     A Text widget which can be scrolled. 
     Text widget with a scrollbar appearing only when you need it 
     (when there is text that you can see) 
     Use self.Text to acccess to your Text widget 
    """ 

    def __init__(self, master=None, cnf={}, **kw): 

     #Creat a Frame which will contain the Text and the Scrollbar widget 
     Frame.__init__(self, master=None, cnf={}, **kw) 
     self.grid_columnconfigure(0, weight=1) 
     self.grid_rowconfigure(0, weight=1) 

     #Creat Scrollbar widget 
     self.Scrollbar=Scrollbar(self, orient='vertical') 

     #Creat Text widget 
     self.Text=Text(self, cnf={}, **kw) 

     #Link between Text and Scrollbar widgets 
     self.Text.config(yscrollcommand=self.Scrollbar.set) 
     self.Scrollbar.config(command=self.Text.yview)  

     #Distribution of the Text widget in the frame 
     self.Text.grid(row=0, column=0, sticky=N+S+E+W) 



     def TypingAndResizing(event): 
      """Check whether you need a scrollbar or not""" 
      if Text.Scrollbar.get()==(0.0, 1.0): 
       self.Scrollbar.grid_forget() 
      else: 
       self.Scrollbar.grid(row=0, column=1, sticky=S+N) 

     self.Text.bind('<KeyRelease>', TypingAndResizing) 
     self.Text.bind('<Configure>', TypingAndResizing) 



root=Tk() 
Text=TextScrollbar(root) 
Text.pack(fill=BOTH, expand=1) 
+0

你確定這是你的真實密碼嗎? 「行」,「列」和「粘性」不是'pack'的有效選項。 –

+0

事實上,這是因爲我嘗試與網格,我忘記改變。它已完成並更新了正確的代碼 –

+0

您的綁定在插入新角色之前發生,因此它總會滯後一點。如果你在''而不是''上綁定,它會更好嗎? –

回答

1

我終於找到了解決方案。我使用.grid而不是.pack。 它不僅解決了問題,而且還處理起來更好!