Reputation: 3584
I'm trying to make a scrollable Text with tkinter widget. I want the scrollbar to appear only when I need it ( when a part of my text widget can't be visible ).
My program searches for every typing if it's the case or not, if it is the scrollbar appears, if it's not it doesn't.
It works well the first time but if I erase some text ( so the scrollbar disappears ), and then write some, the scrollbar appears but without the slider !
#-*-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)
I still don't know why it didn't work but replacing .pack methode by .grid methode it works, here is the code updated
#-*-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)
Upvotes: 2
Views: 1747
Reputation: 3584
I finally found a solution. Instead of .pack I use .grid. Not only does it solve the problem but it is also better to handle the resizement!
Upvotes: 1