Is there any way to put multiple lines of text in each selection part for the tkinter listbox?

Thanks for the help.

Recommended Answers

All 5 Replies

You can put as many lines as you want into a listbox. If there are more lines than the listbox will display, then you also want to add a scrollbar.

import Tkinter                                                                  
top = Tkinter.Tk()                                                              
top.geometry( "100x100+10+10" )                                                 
top.minsize( 200, 200 )                                                         
listbox = Tkinter.Listbox( top, height=6, width=20, font=('Fixed', 14) )        
scrolly = Tkinter.Scrollbar( top, command=listbox.yview )                       
scrollx = Tkinter.Scrollbar( top, orient=Tkinter.HORIZONTAL, command=listbox.xvi
lit = [ "aaa", "bbbbb", "ccccccc", "dd", "e", \                                 
        "fff", "ggggg", "hhhhhhh", "jj", "m", "nn" ]                            
for item in range(10):                                                          
    this_lit = lit[item]                                                        
    new_item = "%(item)d  %(this_lit)-10s  %(this_lit)-10s  %(this_lit)-10s" % v
    listbox.insert(Tkinter.END, new_item)                                       
cont = Tkinter.Button(top, text='CONTINUE',                                     
       command=top.quit, bg='red', fg='white' )                                 
cont.pack(side="bottom", fill=Tkinter.X, expand=1)                              
scrolly.pack(side="right", fill="y")                                            
scrollx.pack(side="bottom", fill=Tkinter.X, expand=1)                           

This might be what you want ...

# a simple Tkinter ListBox example for multiline select
# use ctrl mouse click or shift mouse click

from Tkinter import *

def get_list():
    function to read the listbox selection(s)
    (mutliple lines can be selected)
    and put the result(s) in a label
    # tuple of line index(es)
    sel = listbox1.curselection()
    # get the text, might be multi-line
    seltext = '\n'.join([listbox1.get(x) for x in sel])

root = Tk()
# used in label
choices = StringVar(root)

# extended mode allows CTRL or SHIFT mouse selections
listbox1 = Listbox(root, selectmode = EXTENDED)

# click the button to show the selection(s)
button1 = Button(root, text = "Get Selection(s)", command = get_list)

# display selection(s)
label1 = Label(root, textvariable = choices)

# store some items in the listbox
items = ["one", "two", "three", "four", "five", "six"]
for item in items:
    listbox1.insert(END, item)

# highlight line 3 of listbox (line 1 is zero)
# lb.selection_set(first, last=None) can select more than 1 line


Thanks for the help, but i was thinking more like a multiline display as one selection... not a display of multiple lines or multiple seletions of different lines.

What i meat was is there any way to make a listbox that displayed multiple lines as one block to select.

For example:




where when the user selects one of the 'aaaaa' lines all the aaaaa lines are seleceted and when one of the bbbbb lines is selected, all the bbbbb lines are selected.

Is there any way to do that? Thanks for any help.

The idea of a listbox is to have each list item occupy one line. If you want an item occupy more than one line, you have to go to a grid widget (basis of a spreadsheet). Tkinter has no such widget, but wxPython has a very versatile grid widget with lots of options.

You may also want to look at the radiobutton widget. A slight modification of one of Grayson's examples

from Tkinter import *

root = Tk()

fruit=[('Passion fruit\nSecond line', 1), ('Loganberries', 2), 
        ('Mangoes\nin syrup', 3), ('Oranges', 4), 
        ('Apples', 5), ('Grapefruit', 6)]
var = IntVar()
for text, value in fruit:
    Radiobutton(root, text=text, value=value, variable=var).pack(anchor=W)
Be a part of the DaniWeb community

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