Python GUI Tk : ListBox
Insert image to a List box
import os import Tkinter root = Tkinter.Tk() L = Tkinter.Listbox(selectmode=Tkinter.SINGLE) gifsdict = {} dirpath = '.\' for gifname in os.listdir(dirpath): if not gifname[0].isdigit(): continue gifpath = os.path.join(dirpath, gifname) gif = Tkinter.PhotoImage(file=gifpath) gifsdict[gifname] = gif L.insert(Tkinter.END, gifname) L.pack() img = Tkinter.Label() img.pack() def list_entry_clicked(*ignore): imgname = L.get(L.curselection()[0]) img.config(image=gifsdict[imgname]) L.bind('<ButtonRelease-1>', list_entry_clicked) root.mainloop() '''''''''''''''''''''''''''''''''''''''''''''''''''''''' From Kevin Routley (kbroutley at gmail.com) I have tested with Python 2.7 and 3.5 using Windows 7. There were two bugs in the sample code as presented: 1. The dirpath = '.\' would not work. It needs to be './' or '.\\' 2. The line which starts "img.config" (third from the bottom) needs a leading tab. The description of the sample is misleading. It reads "Insert image to a list box": this implies images are being put in the list box. Tkinter does not support using images inside a list box. The description might be changed to: "View image by selecting name from list." [I was trying the sample because I was seeking to use images in a list box. Research indicates a different widget set will be necessary, e.g. tix or BWidgets.] import os try: import Tkinter except ImportError: import tkinter as Tkinter root = Tkinter.Tk() L = Tkinter.Listbox(selectmode=Tkinter.SINGLE) gifsdict = {} dirpath = '.\\' for gifname in os.listdir(dirpath): if not gifname[0].isdigit(): continue gifpath = os.path.join(dirpath, gifname) gif = Tkinter.PhotoImage(file=gifpath) gifsdict[gifname] = gif L.insert(Tkinter.END, gifname) L.pack() img = Tkinter.Label() img.pack() def list_entry_clicked(*ignore): imgname = L.get(L.curselection()[0]) img.config(image=gifsdict[imgname]) L.bind('<ButtonRelease-1>', list_entry_clicked) root.mainloop()
ListBox with scrollBar
import Tkinter s = Tkinter.Scrollbar() L = Tkinter.Listbox() s.pack(side=Tkinter.RIGHT, fill=Tkinter.Y) L.pack(side=Tkinter.LEFT, fill=Tkinter.Y) s.config(command=L.yview) L.config(yscrollcommand=s.set) for i in range(30): L.insert(Tkinter.END, str(i)*3) Tkinter.mainloop()
Get selected value from Listbox
import Tkinter F1 = Tkinter.Frame() s = Tkinter.Scrollbar(F1) L = Tkinter.Listbox(F1) s.pack(side=Tkinter.RIGHT, fill=Tkinter.Y) L.pack(side=Tkinter.LEFT, fill=Tkinter.Y) s['command'] = L.yview L['yscrollcommand'] = s.set for i in range(30): L.insert(Tkinter.END, str(i)) F1.pack(side=Tkinter.TOP) F2 = Tkinter.Frame() lab = Tkinter.Label(F2) def poll(): lab.after(200, poll) sel = L.curselection() lab.config(text=str(sel)) lab.pack() F2.pack(side=Tkinter.TOP) poll() Tkinter.mainloop()
List Box: insert items
from Tkinter import * root = Tk() root.title('Listbox') list = Listbox(root, width=15) list.pack() for item in range(10): list.insert(END, item) root.mainloop()
List box demo
from Tkinter import * class AllTkinterWidgets: def __init__(self, master): frame = Frame(master, width=500, height=400, bd=1) frame.pack() iframe3 = Frame(frame, bd=2, relief=GROOVE) listbox = Listbox(iframe3, height=4) for line in ['Listbox Entry One','Entry Two','Entry Three','Entry Four']: listbox.insert(END, line) listbox.pack(fill=X, padx=5) iframe3.pack(expand=1, fill=X, pady=10, padx=5) iframe4 = Frame(frame, bd=2, relief=SUNKEN) text=Text(iframe4, height=10, width =65) fd = open('test.py') lines = fd.read() fd.close() text.insert(END, lines) text.pack(side=LEFT, fill=X, padx=5) sb = Scrollbar(iframe4, orient=VERTICAL, command=text.yview) sb.pack(side=RIGHT, fill=Y) text.configure(yscrollcommand=sb.set) root = Tk() all = AllTkinterWidgets(root) root.title('Tkinter Widgets') root.mainloop()
ScrollBar for List box
from Tkinter import * root = Tk() root.title('Scrollbar') list = Listbox(root, height=6, width=15) scroll = Scrollbar(root, command=list.yview) list.configure(yscrollcommand=scroll.set) list.pack(side=LEFT) scroll.pack(side=RIGHT, fill=Y) for item in range(30): list.insert(END, item) root.mainloop()
Scrolled List
from Tkinter import * class ScrolledList(Frame): def __init__(self, options, parent=None): Frame.__init__(self, parent) self.pack(expand=YES, fill=BOTH) self.makeWidgets(options) def handleList(self, event): index = self.listbox.curselection() label = self.listbox.get(index) self.runCommand(label) def makeWidgets(self, options): sbar = Scrollbar(self) list = Listbox(self, relief=SUNKEN) sbar.config(command=list.yview) list.config(yscrollcommand=sbar.set) sbar.pack(side=RIGHT, fill=Y) list.pack(side=LEFT, expand=YES, fill=BOTH) pos = 0 for label in options: list.insert(pos, label) pos = pos + 1 list.bind('<Double-1>', self.handleList) self.listbox = list def runCommand(self, selection): print 'You selected:', selection if __name__ == '__main__': options = map((lambda x: 'Lumberjack-' + str(x)), range(20)) ScrolledList(options).mainloop()