Nictk 2.2.0
An easy to use pyton GUI
Window.py

An example wich creates a Nictk.Window, shows and hides it and can change its mode (normal, modal or persistent).

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 show_window(event):
26 """This is called by butShow and shows the secondary window."""
27 butShow.deactivate()
28 ent1.set_content("")
29 winSec.show()
30
31
32def hide_window(event):
33 """This is called by butDel (or if you click the upperleft close
34 button): hides the secondary window and passes what we typed to
35 the main."""
36 winSec.hide()
37 butShow.activate()
38 s = ent1.get_content()
39 if len(s) > 0:
40 labCont.set_content("You typed: " + s)
41 else:
42 labCont.set_content("You don't type anything")
43
44
45def change_window(event):
46 """This is called by the radiobuttons and sets the secondary
47 window properties (normal, modal or persistent)."""
48 winSec.set_modal(strType.get())
49
50
51# Main window
52winMain = Ntk.Main(200, 150, 600, 450, "Main Window")
53# fill the window with a rowframe for aligning widgets
54rfr1 = Ntk.RowFrame(winMain, 0, 0, "fill", "fill")
55rfr1.config_children("all", fcolor="dark blue", font=("DefaultFont", 12))
56rfr1.config_children(Ntk.Label, bcolor="#E0E0A0", relief="ridge")
57
58rfr1.add_row("10%")
59# button for showing the secondary window
60butShow = Ntk.Button(rfr1, "center", "center", "40%", "80%", content="Show the window",
61 command=show_window)
62# variable for radiobuttons
63strType = Ntk.StringVar()
64
65rfr1.add_row("20%")
66# radiobutton and label for normal window
67radNormal = Ntk.Radiobutton(rfr1, 20, 0, 120, "fill", pad=(10, 10, 0, 10),
68 content=" Normal ", command=change_window)
69radNormal.set_variable(strType, "normal")
70labNormal = Ntk.Label(rfr1, "pack", 0, "fill", "fill", pad=(5, 10, 20, 10),
71 content="The window will be a normal window")
72
73rfr1.add_row("20%")
74# radiobutton and label for modal window
75radModal = Ntk.Radiobutton(rfr1, 20, 0, 120, "fill", pad=(10, 10, 0, 10),
76 content=" Modal ", command=change_window)
77radModal.set_variable(strType, "modal")
78labModal = Ntk.Label(rfr1, "pack", 0, "fill", "fill", pad=(5, 10, 20, 10),
79 content="The window will always be visible, and mantain the focus until you close it")
80
81# radiobutton and label for persistent window
82rfr1.add_row("20%")
83radPers = Ntk.Radiobutton(rfr1, 20, 0, 120, "fill", pad=(10, 10, 0, 10),
84 content="Persistent", command=change_window)
85radPers.set_variable(strType, "persistent")
86labPers = Ntk.Label(rfr1, "pack", 0, "fill", "fill", pad=(5, 10, 20, 10),
87 content="The window will always be visible, but can lose the focus")
88
89# label to see what we typed in secondary window
90rfr1.add_row("25%")
91labCont = Ntk.Label(rfr1, "pack", 0, "fill", "fill", pad=(10, 10, 20, 10))
92labCont.config(anchor="center")
93
94# secondary window
95winSec = Ntk.Window(None, 220, 220, 300, 200, "Secondary Window")
96lbl1 = Ntk.Label(winSec, 0, 0, "fill", "30%", pad=10, content="Type something below")
97lbl1.config(bcolor="#E0E0A0")
98ent1 = Ntk.Entry(winSec, 0, "pack", "fill", "30%", pad=(30, 10))
99ent1.config(bcolor="#F0D0D0")
100butExit = Ntk.Button(winSec, 220, 160, 70, 30, content="Exit", command=hide_window)
101# binds a callback to closing window
102winSec.onclose(hide_window)
103# initially hides secondary window
104winSec.hide()
105# initially sets normal window
106radNormal.invoke()
107
108Ntk.mainloop()