So, it would be nice to be able to insert scrollbars into my program (with Tkinter). Right now I'm using labels to display all my long blocks of text, and people flip through pages to get throught it all.

I thought I'd try to add a scrollbar, but it doesn't seem to work with labels, and I have terrible luck getting canvases to work. Any ideas?

Recommended Answers

All 3 Replies

Don't use a label, use a text widget ...

# just a way to display long text
 
import Tkinter as tk
 
def display(data):
    """creates a text display area with a vertical scrollbar"""
    scrollbar = tk.Scrollbar(root)
    text1 = tk.Text(root, yscrollcommand=scrollbar.set)
    text1.insert(0.0, data)
    scrollbar.config(command=text1.yview)
    scrollbar.pack(side='right', fill='y')
    text1.pack(side='left', expand=0, fill='both')
    
root = tk.Tk()
str1 = """\
A very long story here ...
"""
display(str1)
root.mainloop()

Okay, thanks. I'll give it a shot. The only problem I've found with text boxes so far is that they seem to be sized completely differently from other widgets -- I use the screen width/height to size appropriately for most things, but with texts I have to do a lot of guesswork, since they always come out much bigger.

Oh well, thanks for the advice!

I had the same problem until I figured out that the Text widget width is in characters and the height in text lines. Makes it easier for text, but harder for overall sizing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.