1

I'm trying to get a simple scrollbar to show up on the text widget...I've derived the following code which adds the scrollbar but seems to eliminate the text widget completely.

eula = Tkinter.Text(screen1_2)
eula.insert("1.0", "text")
eula.pack(side="left",fill="both",expand=True)
scroll = Tkinter.Scrollbar(eula)
scroll.pack(side="right",fill="y",expand=False)

1 Answer 1

4

Note that you must define yscrollcommand=scroll.set in the Text widget, and configure the scrollbar using the line: scroll.config(command=eula.yview) Here is your code, rewritten with one scrollbar:

# Import Tkinter
from Tkinter import *

# define master
master = Tk()

# Vertical (y) Scroll Bar
scroll = Scrollbar(master)
scroll.pack(side=RIGHT, fill=Y)

# Text Widget
eula = Text(master, wrap=NONE, yscrollcommand=scroll.set)
eula.insert("1.0", "text")
eula.pack(side="left")

# Configure the scrollbars
scroll.config(command=eula.yview)
mainloop()

Here is a Tkinter example with two scrollbars:

# Import Tkinter
from Tkinter import *

# define master
master = Tk()

# Horizontal (x) Scroll bar
xscrollbar = Scrollbar(master, orient=HORIZONTAL)
xscrollbar.pack(side=BOTTOM, fill=X)

# Vertical (y) Scroll Bar
yscrollbar = Scrollbar(master)
yscrollbar.pack(side=RIGHT, fill=Y)

# Text Widget
text = Text(master, wrap=NONE,
            xscrollcommand=xscrollbar.set,
            yscrollcommand=yscrollbar.set)
text.pack()

# Configure the scrollbars
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)

# Run tkinter main loop
mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

If all you need is a Text widget with a single vertical scrollbar, and you're not trying to solve this for academic reasons, I would recommend using ScrolledText. It is a Text widget with an integrated vertical scrollbar. See docs.python.org/2/library/scrolledtext.html for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.