Tkinter Text Add Scrollbar design
Combining the Scrollbar with the Listbox, we can refer to that section for ideas to apply the Scrollbar to the Text control.
Example#1
Change the original display of only 3 lines of text to display 5 lines of text, in addition to the main application of Scrollbar in the Text control, so that the entire Text text area to increase the y-axis scrollbar.
from tkinter import *
root = Tk()
root.title("apidemos.com")
yscrollbar = Scrollbar(root)
text = Text(root,height=5,width=30)
yscrollbar.pack(side=RIGHT,fill=Y)
text.pack()
yscrollbar.config(command=text.yview)
text.config(yscrollcommand=yscrollbar.set)
myString = \
"""apidemos.com provides tutorials on the basics of programming, introducing the basics of various programming languages such as HTML, CSS, Javascript, Python, Java, Ruby, C, PHP , MySQL and more. The site also provides a large number of online examples, through the examples, you can better learn programming..."""
text.insert(END,myString)
root.mainloop()
Output:
We can now drag the vertical scroll bar down to see more content.
Example#2
Add a scrollbar for the x-axis. Please pay attention to line 8, if you want to show the scrollbar of x-axis you must set wrap="none".
from tkinter import *
root = Tk()
root.title("apidemos.com")
xscrollbar = Scrollbar(root,orient=HORIZONTAL)
yscrollbar = Scrollbar(root)
text = Text(root,height=5,width=30,wrap="none")
xscrollbar.pack(side=BOTTOM,fill=X)
yscrollbar.pack(side=RIGHT,fill=Y)
text.pack()
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
text.config(xscrollcommand=xscrollbar.set)
text.config(yscrollcommand=yscrollbar.set)
myString = \
"""apidemos.com provides tutorials on the basics of programming, introducing the basics of various programming languages such as HTML, CSS, Javascript, Python, Java, Ruby, C, PHP , MySQL and more. The site also provides a large number of online examples, through the examples, you can better learn programming..."""
text.insert(END,myString)
# text.insert(INSERT,myString)
root.mainloop()
Output:
You can drag the horizontal scroll bar left and right to see the complete content.
If we make the window bigger, we can still see the Text text area set. Since we do not use the fill or expand parameters to make further settings, the Text text area will keep the parameter settings of height and width on line 9 and will not change.
When designing the Text text area, if you want this area to change size with the window, you can use the fill and expand parameters in moderation when using pack( ).
Example#3
Let the Text text area expand as the window expands. To make the text area visible, set the background of this area to yellow, as shown in line 9. Line 9 allows the Text text area to expand as the window expands.
from tkinter import *
root = Tk()
root.title("apidemos.com")
xscrollbar = Scrollbar(root,orient=HORIZONTAL)
yscrollbar = Scrollbar(root)
text = Text(root,height=5,width=30,wrap="none",bg="lightyellow")
xscrollbar.pack(side=BOTTOM,fill=X)
yscrollbar.pack(side=RIGHT,fill=Y)
text.pack(fill=BOTH,expand=True) # text.pack(fill=X,expand=True)
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
text.config(xscrollcommand=xscrollbar.set)
text.config(yscrollcommand=yscrollbar.set)
myString = \
"""apidemos.com provides tutorials on the basics of programming, introducing the basics of various programming languages such as HTML, CSS, Javascript, Python, Java, Ruby, C, PHP , MySQL and more. The site also provides a large number of online examples, through the examples, you can better learn programming..."""
text.insert(END,myString)
# text.insert(INSERT,myString)
root.mainloop()
Output: