Beispiel für eine tk.Listbox():
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import Tkinter as tk
5
6
7 class ListboxDemo(object):
8 def __init__(self, master):
9 self.selection = []
10 self.items = ["eins", "zwei", "drei", "und als letztes vier"]
11 activated = (0,2) # Vor-Aktivierte Punkte
12
13 # Kleiner Info Text
14 tk.Label(master, text="Wähle was aus und drücke OK").pack()
15
16 # Die Listbox erstellen
17 self.listbox = tk.Listbox(
18 master,
19 selectmode=tk.MULTIPLE, # multiple choice
20 #selectmode=tk.SINGLE # single select mode
21 height=5, width=80
22 )
23 self.listbox.pack()
24
25 # Einträge einfügen
26 for txt in self.items:
27 self.listbox.insert(tk.END, txt)
28
29 # Vor-Aktivierte Punkte selektieren
30 for index in activated:
31 self.listbox.selection_set(index)
32
33 # Noch zwei Buttons erstellen
34 b = tk.Button(master, text = "OK", command=self.display_selection)
35 b.pack(side=tk.RIGHT)
36 b = tk.Button(master, text = "Abort", command=master.destroy)
37 b.pack(side=tk.RIGHT)
38
39 def display_selection(self):
40 selection = []
41 for i in self.listbox.curselection():
42 index = int(i) # i ist ein String
43 selection.append(self.items[index])
44
45 print "Aktuelle Selektion ist:", selection
46
47 if __name__ == '__main__':
48 root = tk.Tk()
49 root.title("Listbox demo")
50
51 ListboxDemo(root)
52
53 root.mainloop()