Nictk 2.2.0
An easy to use pyton GUI
Spinbox.py

Example of the use of Nictk.Spinbox widget: you can set the mode (normal, readonly and autoadd) and the wrap property.

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
29MODES = {"normal":"You can change the Spinbox value clicking on buttons or typing in it and pressing <RETURN>",
30 "readonly":"You can only use up and down buttons",
31 "autoadd":"As Normal, but every new value you type is added to the Spinbox list"}
32
33WRAPS = ["When you arrive at the end of the list you can continue from the beginning",
34 "When you arrive at the end of the list you cannot go beyond"]
35
36
37def change_label(event):
38 w = event.widget
39 if w == spb1:
40 labSample.config(fcolor="dark blue")
41 labSample.set_content("Selected city: " + w.get_content())
42 else:
43 labSample.config(fcolor="brown")
44 labSample.set_content("Selected number: " + w.get_content())
45
46
47def change_mode(event):
48 opt = optMode.get()
49 labMode.set_content(MODES[opt])
50 spb1.mode(opt)
51
52def change_wrap(event):
53 opt = optWrap.get()
54 labWrap.set_content(WRAPS[0] if opt else WRAPS[1])
55 spb1.config(wrap=opt)
56
57
58winMain = Ntk.Main(200, 150, 640, 480, "Spinbox")
59# main hor frame, containing left and right subframes
60vfr1 = Ntk.HorFrame(winMain, 0, 0, FILL, -60)
61
62#left frame
63rfr1 = Ntk.RowFrame(vfr1, 0, 0, "50%", FILL, content="Spinbox1")
64rfr1.config(bcolor="#B0D0D0", relief=RIDGE )
65rfr1.config_children(ALL, bcolor="#B0D0B0")
66rfr1.config_children(Ntk.Spinbox, rbcolor="#A0C0A0")
67
68rfr1.add_row(40)
69spb1 = Ntk.Spinbox(rfr1, CENTER, 0, "50%", FILL, pad=(0, 3), limits=CITIES,
70 command=change_label)
71
72rfr1.add_row(50)
73labModeTitle = Ntk.Label(rfr1, 0, 0, FILL, FILL, pad=(0, 5),
74 content="Spinbox modes")
75labModeTitle.config(bcolor=rfr1.get_config("bcolor"), relief=FLAT, anchor=S,
76 font=("Arial", 16))
77
78rfr1.add_row("30%")
79vfrMode = Ntk.VerFrame(rfr1, 0, CENTER, "40%", "80%")
80vfrMode.config(relief=SUNKEN)
81
82optMode = Ntk.StringVar()
83tempT = tuple(MODES.keys())
84radMode1 = Ntk.Radiobutton(vfrMode, 0, PACK, FILL, "33%", content=tempT[0],
85 variable=(optMode, tempT[0]), command=change_mode)
86radMode2 = Ntk.Radiobutton(vfrMode, 0, PACK, FILL, "33%", content=tempT[1],
87 variable=(optMode, tempT[1]), command=change_mode)
88radMode3 = Ntk.Radiobutton(vfrMode, 0, PACK, FILL, "33%", content=tempT[2],
89 variable=(optMode, tempT[2]), command=change_mode)
90labMode = Ntk.Label(rfr1, PACK, CENTER, FILL, "80%", pad=(5, 0,10, 0))
91labMode.config(anchor=NW)
92
93rfr1.add_row(50)
94labWrapTitle = Ntk.Label(rfr1, 0, 0, FILL, FILL, pad=(0, 5),
95 content="Enable wrap")
96labWrapTitle.config(bcolor=rfr1.get_config("bcolor"), relief=FLAT, anchor=S,
97 font=("Arial", 16))
98
99rfr1.add_row("30%")
100optWrap = Ntk.BooleanVar()
101vfrWrap = Ntk.VerFrame(rfr1, 0, CENTER, "40%", "80%")
102vfrWrap.config(relief=SUNKEN)
103radWrap1 = Ntk.Radiobutton(vfrWrap, 0, PACK, FILL, "50%", content="True",
104 variable=(optWrap, True), command=change_wrap)
105radWrap2 = Ntk.Radiobutton(vfrWrap, 0, PACK, FILL, "50%", content="False",
106 variable=(optWrap, False), command=change_wrap)
107labWrap = Ntk.Label(rfr1, PACK, CENTER, FILL, "80%", pad=(5, 0,10, 0))
108labWrap.config(anchor=NW)
109
110# right frame
111rfr2 = Ntk.RowFrame(vfr1, PACK, 0, FILL, FILL, content="Spinbox2")
112rfr2.config(bcolor="#FFFFA0", relief=RIDGE)
113rfr2.add_row(40)
114spb2 = Ntk.Spinbox(rfr2, CENTER, 0, "50%", FILL, pad=(0,3),
115 limits=(1.0, 10.0, 0.5), command=change_label)
116
117#bottom label
118labSample = Ntk.Label(winMain, CENTER, PACK, 280, FILL, pad=(0, 5),
119 content="Try the spinboxes!")
120labSample.config(bcolor="pink", anchor=CENTER, font=("Arial", 16))
121
122radMode1.invoke()
123radWrap2.invoke()
124
125Ntk.mainloop()
Definition: constants.py:1