Kyle
Kyle

Reputation: 2379

Python scrollbar on text widget

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)

Upvotes: 1

Views: 18773

Answers (1)

bitoffdev
bitoffdev

Reputation: 3384

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()

Upvotes: 4

Related Questions