Scrolling line number Tkinter

Martin Franklin martin.franklin at westerngeco.com
Tue Mar 19 05:56:31 EST 2002


Fabien Henon wrote:

> Martin Franklin wrote:
> 
>> Fabien Henon wrote:
>> 
>> 
>>>I am currently writing a text editor for POV-RAY under Windows using
>>>Tkinter. It should be working as well with linux and Mac.
>>>
>>>To display the line numbers, I use a scrolling column at the left of the
>>>editor with a text box.
>>>
>> 
>> Can you please explain what a scrollng column is? Perhaps if you post
>> some code we could help some more.
>> 
>> 
>> 
>>>I also linked the main text editor with a scroll.
>>>
>>>My problem is that the text editor is well linked to the scroll, but the
>>>line numbers do not move when I use the arrow cursors.
>>>
>>>Thanks for any help
>>>
>>>Fabien
>>>
>> 
>> It sounds like you need to link both the scrolled column and text box to
>> the same scrollbar....
>> 
> What I mean by scrolling column is a column in which are displayed all the
> line number of the text.
> 
> I want this column of lines to scroll with the edited text.
> 
> 
> cheers.

By Column you mean Frame widget?

It's wierd I've just finished writing a Text box with an optional line 
number counter down the side....... basically the code boils down to this:-



f=Frame(self)
# self.textaree is the main text box (also happens to be a Pmw 
# ScroledText so I get dynamic scrollbars for free!
self.textarea=Pmw.ScrolledText(f, text_wrap='none')

# self.lineCounter is a Tk.Text widget I grid inside the 
# Pmw.ScrolledTextBox above so I can share the Pmw dynamic 
# scrollbars
#

self.lineCounter=Text(self.textarea.interior(), width=5, bg='grey', 
    state='disabled')
self.textarea.length=int(self.textarea.index('end').split('.')[0])
       
# I only bind the main textarea to the scroll bars cause it caused lots 
# of problems when I tried to bind both Text widgets.....
# this is taken care of whe I update the line counter...
self.textarea.component('text').config(yscrollcommand=self.yscrollset)
self.textarea._vertScrollbar['command'] = self.yscroll
        



# Scroll functions.....

    
def yscrollset(self, *stuff):
    ## print 'yscrollset called', stuff
    apply(self.textarea._vertScrollbar.set, stuff)
    self.lineCounter.yview_moveto(stuff[0])
    
    
def yscroll(self, *stuff):
    ## print 'yscroll called', stuff
    apply(self.textarea._textbox.yview, stuff)
    apply(self.lineCounter.yview, stuff)


def setLineCounter(self, *event):
    # should be called check_line_count
    # if the length of the main textarea (self.textarea) changes
    # then wake up and update the line counter
    new_length=int(self.textarea.index('end').split('.')[0])
    if new_length!=self.textarea.length:
        self.textarea.length=new_length
        self.updateLineCounter()
                
        # need to set scroll pos.... now? how do I do that__?
        apply(self.yscrollset, self.textarea._vertScrollbar.get())
                
    self.after(1, self.setLineCounter) # recurse.....



def updateLineCounter(self, *event):
    self.lineCounter.config(state='normal')
    self.lineCounter.delete('0.0', 'end')
    for line in range(1, self.textarea.length-1):
        self.lineCounter.insert('end', '%i\n' %line)
    self.lineCounter.insert('end', '%i' %(self.textarea.length-1))
    line, col = map(int, self.textarea.index("insert").split("."))
    self.lineCounter.mark_set("insert", "%d.0" %(line))
    self.lineCounter.config(state='disabled')
        




HTH,
Martin.





More information about the Python-list mailing list