Nictk 2.2.0
An easy to use pyton GUI
Menu.py

Various forms of menus: menubar, combobox, popup.

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
26# callbacks (you may prefer to use lambdas)
27def hitmenu(event):
28 labText.set_content("You have chosen '" + event.value + "' from the menu in the menubar")
29
30def hitmenubutton(event):
31 labText.set_content("Edit is " + editon.get())
32
33def hitcolors(event):
34 labText.config(bcolor=event.value)
35
36def hitpopup(event):
37 labText.set_content("You have chosen '" + event.value + "' from the popup menu")
38
39def hitcombo(event):
40 labText.set_content("You have chosen '" + event.value + "' from the combobox")
41
42
43
44winMain = Ntk.Main(200, 180, 640, 480, "Menu example")
45winMain.config_children(Ntk.Label, relief=SOLID, borderwidth=1)
46# menu bar (auto added to the parent winMain)
47menuBar = Ntk.Menu(winMain)
48# first menu (auto added to the parent menuBar)
49menuFile = Ntk.Menu(menuBar, "File")
50menuFile.add_command(label="Open", command=(hitmenu, "Open"))
51menuFile.add_command(label="Save", command=(hitmenu, "Save"))
52menuFile.add_command(label="Save as ...", command=(hitmenu, "Save as ..."))
53menuFile.add_separator()
54menuFile.add_command(label="Quit", command=lambda event: winMain.destroy())
55# second menu (auto added to the parent menuBar)
56menuEdit = Ntk.Menu(menuBar, "Edit")
57menuEdit.add_checkbutton(label="Edit on", command=hitmenubutton)
58# sets the variable for the checkbutton (you can use as index the number (from 0)
59# or the label
60editon = Ntk.StringVar()
61menuEdit.entry_set_variable("Edit on", variable=editon, offvalue="off", onvalue="on")
62# third menu
63menuColors = Ntk.Menu(menuBar, "Colors")
64menuColors.add_command(label="Change to green", command=(hitcolors, "light green"))
65menuColors.add_command(label="Change to pink", command=(hitcolors, "pink"))
66menuColors.add_command(label="Change to blue", command=(hitcolors, "light blue"))
67# menu entries option config. You can use as index the number (from 0) or the label
68menuColors.entry_config(0, bcolor="light green")
69menuColors.entry_config(1, bcolor="pink")
70menuColors.entry_config(2, bcolor="light blue")
71
72# popup menu (not added to the parent because popup is True)
73menuPopup = Ntk.Menu(winMain, popup=True)
74menuPopup.add_command(label="Cut", command=(hitpopup, "Cut"))
75menuPopup.add_command(label="Copy", command=(hitpopup, "Copy"))
76menuPopup.add_command(label="Paste", command=(hitpopup, "Paste"))
77
78# creates the upper label and binds double click on it to popup opening
79labPopup=Ntk.Label(winMain, 0, "10%", "fill", 100, pad=(15, 10), content="Double click to open a popup", )
80labPopup.bind("<Double-Button-1>", lambda event: menuPopup.post(event.x_root, event.y_root))
81labPopup.config(bcolor="orange", anchor=CENTER, font=("Arial", 12, "bold"))
82
83#creates the middle Combobox
84cmbmenu = ["Option " + str(i + 1) for i in range(100)]
85cmbSample = Ntk.Combobox(winMain, CENTER, PACK, "80%", 50, pad=(0, 5), items=cmbmenu, command=hitcombo)
86cmbSample.config(fcolor="dark blue", afcolor="blue", font=("Arial", 12, "bold"))
87
88#creates the lower label
89labText = Ntk.Label(winMain, PACK, PACK, FILL, FILL, pad=(15, 10))
90labText.config(bcolor="light green", fcolor="dark green", font=("Arial", 16), anchor=CENTER)
91
92Ntk.mainloop()
Definition: constants.py:1