Nictk 2.2.0
An easy to use pyton GUI
Listbox.py

Example of the use of the Nictk.Listbox widget, with various operations on it and showing its auto add scrollbar feature.

1# This file is part of Nictk - A simple tkinter wrapper.
2# Copyright (C) 2021-2024 Nicola Cassetta
3# See <https://github.com/ncassetta/Nictk>
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as published
7# by the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU Lesser General Public License for more details.
14#
15# You should have received a copy of the Lesser GNU General Public License
16# along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18
19# Allows import from parent folder. You can delete this if you install the package
20import _setup
21
22import Nictk as Ntk
23from Nictk.constants import *
24
25
26# TODO: delete should delete all items selected, not only the last one
27
28
29# items to be added to the listbox (italian numbers)
30numbers = ("Uno", "Due", "Tre", "Quattro", "Cinque", "Sei", "Sette", "Otto",
31 "Nove", "Dieci", "Undici", "Dodici", "Tredici", "Quattordici")
32# selection modes
33modes = ("single", "browse", "multiple", "extended")
34# explanations of selection modes
35explmodes = ("single mode : you can select only one item at once, clicking on it in the listbox",
36 "browse mode : you can select only an item at once, clicking on it or dragging with the mouse",
37 "multiple mode : you can select multiple items, clicking on them; clicking on a selected item " +
38 "unselects it",
39 "extended mode : you can select multiple items, clicking, dragging the mouse and using the" +
40 "<CTRL> or <SHIFT> keys")
41
42
43def add_item(event):
44 """Adds an item to the left listbox and controls
45 the state of ButAdd and ButDel."""
46 nitems = lstTest.size()
47 if nitems < len(numbers):
48 lstTest.add(numbers[nitems])
49 nitems = lstTest.size()
50 if nitems > 0:
51 butDel.activate()
52 if nitems == len(numbers):
53 butAdd.deactivate()
54
55def del_item(event):
56 """ Deletes an item from the left listbox and controls
57 the state of ButAdd and ButDel."""
58 nitems = lstTest.size()
59 if nitems > 0:
60 lstTest.delete(nitems - 1)
61 # adjusts the selection report
62 lbox_changed(None)
63 nitems = lstTest.size()
64 if nitems < len(numbers):
65 butAdd.activate()
66 if lstTest.size() == 0:
67 butDel.deactivate()
68
69
70
71def lbox_changed(event):
72 """Callback called when you select one or more item in the left listbox."""
73 sel = lstTest.get_selected()
74 #print("testchanged with index", sel)
75 if len(sel) == 0:
76 s = "none"
77 else:
78 s = ""
79 for i in sel:
80 s += "{}, ".format(i)
81 s = s[:-2]
82 labSel.set_content("Selected index: " + s if len(sel) <= 1 else "Selected indexes: " + s)
83
84def mode_changed(event):
85 """Callback called when the user modifies the selection mode."""
86 #lstTest.select_clear(0, END)
87 lbox_changed(None)
88 sel = lstMode.get_selected()
89 #print("selchanged() called with index", sel)
90 if len(sel):
91 labExplain.set_content(explmodes[sel[0]])
92 lstTest.config(selectmode=modes[sel[0]])
93
94
95winMain = Ntk.Main(200, 150, 600, 450, "NtkListbox widget sample")
96
97# we use frames for positioning widgets
98hfr1 = Ntk.HorFrame(winMain, 0, 0, FILL, FILL)
99rfr1 = Ntk.RowFrame(hfr1, 0, 0, "50%", FILL)
100vfr2 = Ntk.VerFrame(hfr1, PACK, 0, FILL, FILL)
101
102rfr1.add_row(40)
103labTest = Ntk.Label(rfr1, 0, 0, FILL, FILL, pad=(10, 10, 10, 5),
104 content="Try to select items")
105labTest.config(bcolor="light green", fcolor="blue", relief=SOLID,
106 borderwidth=1, anchor=CENTER)
107
108rfr1.add_row(-80)
109lstTest = Ntk.Listbox(rfr1, 0, 0, FILL, FILL, pad=(10, 5, 10, 40),
110 command=lbox_changed)
111lstTest.config(bcolor="blue", fcolor="yellow", sfcolor="maroon", sbcolor="light blue",
112 relief=RIDGE, font=("TkDefaultFont", 14))
113
114rfr1.add_row(40)
115labSel = Ntk.Label(rfr1, 0, 0, FILL, FILL, pad=(10, 5))
116labSel.config(bcolor="light green", fcolor="blue", relief=SOLID,
117 borderwidth=1)
118
119# calls the callback to adjust labSel content
120lbox_changed(None)
121
122rfr1.add_row(FILL)
123butAdd = Ntk.Button(rfr1, "15%", 0, "35%", FILL, pad=(5,5, 5, 10),
124 content="Add item", command=add_item)
125butDel = Ntk.Button(rfr1, PACK, PACK, "35%", FILL, pad=(5, 5, 5, 10),
126 content="Del item", command =del_item)
127butDel.deactivate()
128
129labMode = Ntk.Label(vfr2, 0, 0, FILL, 40, pad=(10, 10, 10, 5),
130 content="Listbox Mode")
131labMode.config(bcolor="light green", fcolor="blue", relief=SOLID, borderwidth=1,
132 anchor=CENTER)
133lstMode = Ntk.Listbox(vfr2, 0, PACK, FILL, 120, pad=(10, 5),
134 command=mode_changed, items=modes)
135lstMode.config(bcolor="cyan", fcolor="brown", font=("TkDefaultFont", 14), relief=RIDGE)
136labExplain=Ntk.Label(vfr2, 0, PACK, FILL, FILL, pad=(10, 10),
137 content=explmodes[0])
138labExplain.config(anchor=NW)
139lstMode.select(0)
140
141Ntk.mainloop()
Definition: constants.py:1