Is there any way to put multiple lines of text in each selection part for the tkinter listbox?
Thanks for the help.
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 ) listbox.configure(yscrollcommand=scrolly.set) scrollx = Tkinter.Scrollbar( top, orient=Tkinter.HORIZONTAL, command=listbox.xvi listbox.configure(xscrollcommand=scrollx.set) 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) listbox.pack(side="left") top.mainloop()
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]) choices.set(seltext) root = Tk() # used in label choices = StringVar(root) # extended mode allows CTRL or SHIFT mouse selections listbox1 = Listbox(root, selectmode = EXTENDED) listbox1.pack() # click the button to show the selection(s) button1 = Button(root, text = "Get Selection(s)", command = get_list) button1.pack() # display selection(s) label1 = Label(root, textvariable = choices) label1.pack() # 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 listbox1.selection_set(3) root.mainloop()
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.
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() root.title('Radiobutton') 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) var.set(3) root.mainloop()