Nictk 2.2.0
An easy to use pyton GUI
Combobox.py

Example of various operations on the Nictk.Combobox widget.

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# italian cities
26CITIES = ("Bari", "Bologna", "Firenze", "Milano", "Napoli", "Palermo",
27 "Roma", "Torino", "Venezia")
28
29
30def change_label(event):
31 """This is called when you select a city in the upper combo;
32 it prints the selected city on the helper label."""
33 sel = cmbCities.get_content()
34 labHelp.set_content("You selected: " + sel)
35
36
37def delete_sel(event):
38 """This is called by ButDel; it deletes the selected city
39 from the upper combo and adds it to the lower."""
40 # gets the selected string
41 sel = cmbCities.get_content()
42 if len(sel):
43 # gets its numeric index (from 0)
44 ind = cmbCities.index(sel)
45 # deletes from upper combo (cmbCities.delete(sel) also works)
46 cmbCities.delete(ind)
47 # adds the string to the lower combo
48 cmbIns.add(sel)
49 if cmbCities.get_menu().size() > 0:
50 # selects the next city in the upper combobox (if sel was the last string
51 # the menu auto adjust the index, so no out of range errors)
52 cmbCities.set_content(cmbCities.get_item(ind))
53 labHelp.set_content("You deleted {} (but it is now in the lower combobox".format(sel))
54 else:
55 # the upper combo is empty: selection automatically cleared
56 labHelp.set_content("Empty menu")
57
58
59def insert_sel(event):
60 """This is called by ButIns; it deletes a city in the lower combo
61 and adds it to the upper."""
62 sel = cmbIns.get_content()
63 if len(sel):
64 # see delete_sel
65 ind = cmbIns.index(sel)
66 cmbCities.add(sel)
67 cmbIns.delete(sel)
68 labHelp.set_content("You newly added {} to the upper combobox".format(sel))
69 if cmbIns.get_menu().size() > 0:
70 cmbIns.set_content(cmbIns.get_item(ind))
71 # otherwise selection automatically cleared
72
73
74def reset_combo(event):
75 """This callback resets the contents of the two combos"""
76 # empties and refills the upper combo
77 cmbCities.clear()
78 cmbCities.add(CITIES)
79 # selects the first city
80 cmbCities.set_content(CITIES[0])
81 # empties the lower combo
82 cmbIns.clear()
83 labHelp.set_content("You reset the combos")
84
85
86winMain = Ntk.Main(200, 150, 400, 300, "Combobox sample")
87# configures all children widget when they are created
88winMain.config_children(ALL, font=("Arial", 16))
89winMain.config_children((Ntk.Button, Ntk.Combobox), bcolor="#D0D080", abcolor="#E0E090")
90# creates the combobox with the cities
91cmbCities = Ntk.Combobox(winMain, 0, 0, FILL, 70, pad=(10, 20, 10, 10),
92 items=CITIES, command=change_label)
93# creates the label under it
94labHelp = Ntk.Label(winMain, 0, PACK, FILL, 90, pad=10,
95 content="Initially selected: " + CITIES[0])
96labHelp.config(bcolor="#B0D0F0", relief=RIDGE, anchor=CENTER)
97# rowframe for aligning other widgets
98rfr1 = Ntk.RowFrame(winMain, 0, PACK, FILL, FILL)
99rfr1.add_row(55)
100# button for deleting a city from cmbCities
101butDel = Ntk.Button(rfr1, 0, 0, "50%", FILL, pad=(10, 10, 10, 5),
102 content="Delete selected", command=delete_sel)
103# button for reset
104butRes = Ntk.Button(rfr1, PACK, 0, FILL, FILL, pad=(10, 10, 10, 5),
105 content="Reset", command=reset_combo)
106rfr1.add_row(55)
107# button for reinserting a deleted city
108butIns = Ntk.Button(rfr1, 0, 0, "50%", FILL, pad=(10, 5, 10, 10),
109 content="Insert selected", command=insert_sel)
110butIns.deactivate()
111# combobox for deleted cities
112cmbIns = Ntk.Combobox(rfr1, PACK, 0, FILL, FILL, pad=(10, 5, 10, 10))
113# auto activate - deactivate buttons
114cmbCities.bind("<<ChangedVar>>",
115 lambda ev: butDel.activate() if len(ev.widget.get_content()) else butDel.deactivate())
116cmbIns.bind("<<ChangedVar>>",
117 lambda ev: butIns.activate() if len(ev.widget.get_content()) else butIns.deactivate())
118
119Ntk.mainloop()
Definition: constants.py:1