Nictk 2.2.0
An easy to use pyton GUI
WidgetOptions.py

An example of the most common widget options.

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 tkinter import font
24from random import randrange
25
26COLORS = ("white", "green", "blue", "yellow", "pink", "grey", "black",
27 "cyan", "red", "orange", "gold", "magenta", "purple")
28RELIEFS = ("sunken", "flat", "raised", "groove", "ridge", "solid")
29ANCHORS = ("n", "e", "nw", "se", "s", "sw", "w", "ne", "center")
30JUSTIFIES = ("left", "center", "right")
31CURSORS = ("arrow", "circle", "clock", "cross", "dotbox", "exchange",
32 "fleur", "heart", "man", "mouse", "pirate", "plus",
33 "shuttle", "sizing", "spider", "spraycan", "target",
34 "tcross", "trek", "watch")
35
36
37# callbacks (you may prefer to use lambdas)
38def change_bgcolor(event):
39 labSample.config(bcolor=spbBgcolor.get_content())
40
41
42def change_fgcolor(event):
43 labSample.config(fcolor=spbFgcolor.get_content())
44
45
46def change_relief(event):
47 labSample.config(relief=spbRelief.get_content())
48
49
50def change_border(event):
51 labSample.config(borderwidth=int(spbBorder.get_content()))
52
53
54def change_anchor(event):
55 labSample.config(anchor=spbAnchor.get_content())
56
57
58def change_justify(event):
59 labSample.config(justify=spbJustify.get_content())
60
61
62def change_font(event):
63 fsize = randrange(12, 25)
64 labSample.config(font=(spbFont.get_content(), fsize))
65
66def change_cursor(event):
67 labSample.config(cursor=spbCursor.get_content())
68
69
70# create the main window
71winMain = Ntk.Main(200, 180, 640, 480, "Widget attributes")
72# fill it with a rowframe
73rfr1 = Ntk.RowFrame(winMain, 0, 0, "fill", "fill")
74# main label with the sample phrase
75rfr1.add_row(120)
76labSample = Ntk.Label(rfr1, 0, 0, "fill", "fill", pad=10,
77 content="This is a sample, because\nit demonstrates widget options")
78# you must have an already created window to get fonts
79FONTS = font.families()[:min(15, len(font.families()))]
80# config other children widgets
81rfr1.config_children(Ntk.Label, bcolor="#E0F0C0", relief="solid", border=1)
82rfr1.config_children(Ntk.Spinbox, rbcolor="#A0C0D0", relief="ridge", state="readonly")
83
84# other widgets
85rfr1.add_row(50)
86rfr1.add_row(34)
87labBgcolor = Ntk.Label(rfr1, 0, 0, "20%", "fill", pad=(10,2,2,2), content="bcolor")
88spbBgcolor = Ntk.Spinbox(rfr1, "pack", 0, "30%", "fill", pad=(2,2,10,2), limits=COLORS,
89 command=change_bgcolor)
90labFgcolor = Ntk.Label(rfr1, "pack", 0, "20%", "fill", pad=(10,2,2,2), content="fcolor")
91spbFgcolor = Ntk.Spinbox(rfr1, "pack", 0, "30%", "fill", pad=(2,2,10,2), limits=COLORS,
92 command=change_fgcolor)
93
94rfr1.add_row(34)
95labRelief = Ntk.Label(rfr1, 0, 0, "20%", "fill", pad=(10,2,2,2), content="relief")
96spbRelief = Ntk.Spinbox(rfr1, "pack", 0, "30%", "fill", pad=(2,2,10,2), limits=RELIEFS,
97 command=change_relief)
98labBorder = Ntk.Label(rfr1, "pack", 0, "20%", "fill", pad=(10,2,2,2), content="border")
99spbBorder = Ntk.Spinbox(rfr1, "pack", 0, "30%", "fill", pad=(2,2,10,2), limits=(0, 6, 1),
100 command=change_border)
101
102rfr1.add_row(34)
103labAnchor = Ntk.Label(rfr1, 0, 0, "20%", "fill", pad=(10,2,2,2), content="anchor")
104spbAnchor = Ntk.Spinbox(rfr1, "pack", 0, "30%", "fill", pad=(2,2,10,2), limits=ANCHORS,
105 command=change_anchor)
106labJustify = Ntk.Label(rfr1, "pack", 0, "20%", "fill", pad=(10,2,2,2), content="justify")
107spbJustify = Ntk.Spinbox(rfr1, "pack", 0, "30%", "fill", pad=(2,2,10,2), limits=JUSTIFIES,
108 command=change_justify)
109
110rfr1.add_row(34)
111labFont = Ntk.Label(rfr1, 0, 0, "20%", "fill", pad=(10,2,2,2), content="font")
112spbFont = Ntk.Spinbox(rfr1, "pack", 0, "30%", "fill", pad=(2,2,10,2), limits=FONTS,
113 command=change_font)
114labCursor = Ntk.Label(rfr1, "pack", 0, "20%", "fill", pad=(10,2,2,2), content="cursor")
115spbCursor = Ntk.Spinbox(rfr1, "pack", "pack", "30%", "fill", pad=(2,2,10,2), limits=CURSORS,
116 command=change_cursor)
117
118# call the callbacks to set initial values
119change_anchor(None)
120change_bgcolor(None)
121spbBorder.set_content("2")
122change_cursor(None)
123spbFgcolor.invoke("buttonup")
124change_font(None)
125change_justify(None)
126change_relief(None)
127
128Ntk.mainloop()