I'm trying to program a simple GUI using the Text widget of TkInter and I'm stuck with a strange behavior when I scroll within it.
When I reach the end of the text, sometime, I get a blank line at the bottom of the widget (and sometime not)...
Here you can see the blank line :
Or course I don't want to see this blank line.
Here is a simple snippet that illustrate my issue. Play with it : scroll up and down using the up/down arrows, the home/end/pageup/pagedown keys. At one time, you should see the blank line...
import sys,Tkinter
escape,up,down,home,end,pageup,pagedown = 27,38,40,36,35,33,34
class HText ( Tkinter.Text ):
def __init__(self,parent,lines,w,h ):
Tkinter.Text.__init__( self,parent )
self.config( takefocus=True,width=w,height=h )
self._len = len( lines )
self._index = 0
self.config( state=Tkinter.NORMAL )
for i,t in enumerate( lines ):
self.insert( Tkinter.END,'%-4d : %s\n'%(i,t),'#%d'%i )
self.focus( 1 )
def focus (self,on):
self.tag_config( '#%d'%self._index,background='#daf' if on else 'white' )
def move (self,keycode):
delta = { up:-1,down:+1,home:-self._len,end:self._len,pageup:-20,pagedown:+20 }.get( keycode,0 )
index = max( 0,min( self._index+delta,self._len-1 ))
if index != self._index :
self.focus( 0 )
self._index = index
self.see( '#%d.first'%self._index )
self.focus( 1 )
def Gui ( text ):
self = Tkinter.Tk()
self.bind('<Escape>',lambda e : self.quit())
self.bind('<Key>' ,lambda e : htext.move( e.keycode ))
htext = HText( self,text.split('\n'),120,30 )
htext.pack()
self.mainloop()
self.destroy()
Gui( open( sys.argv[0] ).read()*10 )
Do you see what I'm doing wrong ? Is it a bug in TkInter ?
By the way, I'm using python 2.7 under Windows 7 x64.
Thank in advance for your time.
Hadrien
After the string insertion is done use this line to remove the last newline character:
htext.delete('end-1c', 'end')