Nictk 2.2.0
An easy to use pyton GUI
Scale.py

Example of the use of Nictk.Scale 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
26def changecolor(event):
27 """This is called when you move one fo the scales. It changes
28 the color of the right label background according to their
29 values."""
30 R, G, B = int(sclRed.get_value()), int(sclGreen.get_value()), int(sclBlue.get_value())
31 # string in decimal values "(R, G, B)" to be shown in the label
32 deccolor = "({:3d}, {:3d}, {:3d})".format(R, G, B)
33 # string in hex values for config "#RRGGBB"
34 hexcolor = "#{:02X}{:02X}{:02X}".format(R, G, B)
35 # changes the background color
36 labColor.config(bcolor=hexcolor)
37 # sets the text color white if background is dark or black if it is light
38 labfg = "white" if (R + G + B) / 3 < 90 else "black"
39 labColor.config(fcolor=labfg)
40 labColor.set_content("Color:\n" + deccolor)
41
42
43winMain = Ntk.Main(200, 150, 400, 300, "Scale widget sample")
44winMain.config_children(ALL, relief="solid", borderwidth=1, anchor=CENTER)
45
46# vertical frame for aligning the three scales and their labels in the left side
47vfr1 = Ntk.VerFrame(winMain, 0, 0, "50%", FILL)
48
49# red label and scale
50labRed = Ntk.Label(vfr1, 0, 0, FILL, 45, pad=(10, 15, 20, 5), content="Red")
51sclRed = Ntk.Scale(vfr1, 0, PACK, FILL, 50, pad=(10, 0, 20, 5),
52 limits=(0,255,1), command=changecolor)
53# bcolor: background and non selected cursor color
54# tcolor (through color): color of the cursor guide
55# abcolor: (activebackground color): color of the selected cursor
56sclRed.config(bcolor="red", tcolor="#FF8080", abcolor="#FFA0A0")
57
58# green label and scale
59labGreen = Ntk.Label(vfr1, 0, PACK, FILL, 45, pad=(10, 15, 20, 5),
60 content="Green")
61sclGreen = Ntk.Scale(vfr1, 0, PACK, FILL, 50, pad=(10, 0, 20, 5),
62 limits=(0,255,1), command=changecolor)
63sclGreen.config(bcolor="green", tcolor="#80FF80", abcolor="#A0FFA0")
64
65# blue label and scale
66labBlue = Ntk.Label(vfr1, 0, PACK, FILL, 45, pad=(10, 15, 20, 5),
67 content="Blue")
68sclBlue = Ntk.Scale(vfr1, 0, PACK, FILL, 50, pad=(10, 0, 20, 5),
69 limits=(0,255,1), command=changecolor)
70sclBlue.config(bcolor="blue", tcolor="#8080FF", abcolor="#A0A0FF")
71
72# color label in the right side
73labColor = Ntk.Label(winMain, "50%", 0, FILL, FILL, pad=15)
74labColor.config(anchor=CENTER, justify=CENTER)
75
76# call the callback for setting initial color to black
77changecolor(None)
78
79Ntk.mainloop()
Definition: constants.py:1