Nictk 2.2.0
An easy to use pyton GUI
RCbuttons.py

An example of the use of classes Nictk.Checkbutton and Nictk.Radiobutton widgets. They have both a Variable object associated (which automatically updates a label) and a callback with command option. We used a Nictk.RowFrame and the strings "pack", "fill", etc. (alternative to constants PACK, FILL ...) for positioning the widgets.

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
23
24
25def change_label_text(ev):
26 """This callback recognizes which widget called it,
27 and updates the upper label content accordingly."""
28 if ev.widget == chk1:
29 s = "Pink button variable value: " + chk1Var.get()
30 elif ev.widget == chk2:
31 s = "Orange button variable value: " + str(chk2Var.get())
32 else:
33 s = "Option " + str(optVar.get()) + " selected"
34 labSample.set_content(s)
35
36
37
38winMain = Ntk.Main(200, 150, 600, 400, "Radio and Check Buttons")
39winMain.config(bcolor="#FFFFA0")
40winMain.config_children("all", relief="groove")
41
42# we dispose our widgets in rows
43rfr1 = Ntk.RowFrame(winMain, 0, 0, "fill", "fill")
44rfr1.add_row(60)
45
46# main label: its content is updated by the callback
47labSample = Ntk.Label(rfr1, 0, 0, "fill", "fill", pad=10)
48rfr1.add_row(80)
49
50# creates pink checkbutton, and associates a StringVar to the button states
51chk1Var = Ntk.StringVar()
52chk1 = Ntk.Checkbutton(rfr1, 0, 0, "40%", "fill", pad=(10, 5, 5, 5),
53 content="This button controls a StrVar, which can get values: 'Value: on' and 'Value: off",
54 variable=(chk1Var, "Value: on", "Value: off"), command=change_label_text)
55chk1.config(bcolor="pink", abcolor="pink")
56chk1.select()
57# the label will be automatically updated with chk1Var content
58labChk1 = Ntk.Label(rfr1, "pack", 0, "fill", "fill", pad=(5, 5, 10, 5), content=chk1Var)
59rfr1.add_row(80)
60
61# creates orange checkbutton, and associates an IntVar to the button states
62# (its offvalue and onvalue are set to 0 and 1)
63chk2Var = Ntk.IntVar()
64chk2 = Ntk.Checkbutton(rfr1, 0, 0, "40%", "fill", pad=(10, 5, 5, 5),
65 content="This button controls an IntVar, which can get values: 0 and 1",
66 variable=chk2Var, command=change_label_text)
67chk2.config(bcolor="orange", abcolor="orange")
68chk2.deselect()
69# the label will be automatically updated with chk2Var content
70labChk2 = Ntk.Label(rfr1, "pack", 0 , "fill", "fill", pad=(5, 5, 10, 5), content=chk2Var)
71rfr1.add_row(-10)
72
73# we need this to stack radiobuttons vertically
74frm3 = Ntk.VerFrame(rfr1, 10, 10, -10, "fill")
75frm3.config(relief="groove")
76frm3.config_children("all", relief="flat")
77
78# common variable for all radiobuttons
79optVar = Ntk.IntVar()
80# radiobuttons
81rad1 = Ntk.Radiobutton(frm3, 0, "pack", "fill", "33%", content="Option 1",
82 variable=(optVar, 1), command=change_label_text)
83rad2 = Ntk.Radiobutton(frm3, 0, "pack", "fill", "33%", content="Option 2",
84 variable=(optVar, 2), command=change_label_text)
85rad3 = Ntk.Radiobutton(frm3, 0, "pack", "fill", "33%", content="Option 3",
86 variable=(optVar, 3), command=change_label_text)
87# sets rad2 at the beginning
88rad2.invoke()
89# clears the text set by the previous
90labSample.set_content("")
91
92Ntk.mainloop()