Right-click Menu
Sept 29, 2020 7:29:53 GMT -8
Post by Uncle Buddy on Sept 29, 2020 7:29:53 GMT -8
# right_click_menu (import as rcm)
import tkinter as tk
import styles as st
import widgets as wdg
from message_strings import make_header_roles_notes
ST = st.ThemeStyles()
def make_rc_menus(
rcm_widgets,
rc_menu,
rcm_msg,
header_parent=None,
dlg_header=None,
which_dlg=None):
'''
To include a widget in the right-click context help, list the widget
in rcm_widgets in the instance and store each widget's message
and title in message_strings.py. Example of usage from notes.py: near
bottom of make_widgets() i.e. after making all widgets, do this...
rcm_widgets = (self.subtopic_input.ent, self.note_input.text)
rcm.make_rc_menus(
rcm_widgets,
self.rc_menu,
ms.note_dlg_msg,
header_parent=self.notes_dialog_header,
dlg_header=self.dlg_header,
which_dlg='notes')
...and in __init__ right before calling make_widgets() do this...
self.rc_menu = rcm.RightClickMenu(self.root)
The 3 default values can be ignored except where data is being passed
that will be used to make headers in dialogs and the header items will
have right-click context help functionality.
'''
make_header_roles_notes(header_parent, dlg_header, which_dlg, rc_menu)
rc_menu.help_per_context = dict(zip(rcm_widgets, rcm_msg))
for widg in rcm_widgets:
widg.bind("<Button-3>", rc_menu.attach_rt_clk_menu)
for k,v in rc_menu.loop_made.items():
k.bind("<Button-3>", rc_menu.attach_rt_clk_menu)
rc_menu.help_per_context[k] = v
class RightClickMenu(tk.Menu):
def __init__(self, master, **options):
tk.Menu.__init__(self, master, **options)
self.master = master
self.message = ''
self.help_title = ''
self.widg = None
self.help_per_context = {}
self.config(tearoff=0)
self.loop_made = {}
self.add_command(label='Copy', command=self.copy)
self.add_command(label='Paste', command=self.paste)
self.add_separator()
self.add_command(label='Context Help', command=self.context_help)
# # EXAMPLE don't delete, this is how you config() the menu items
# # post-constructor, or do it in the instance, see below.
# self.entryconfigure('Copy', state='disabled')
def copy(self):
print('Copied')
def paste(self):
print('Pasted')
def context_help(self):
help = wdg.Toplevel()
help.title(self.help_title)
text = wdg.LabelStylable(
help,
width=75)
text.grid(padx=24, pady=24)
text.insert('end', self.message)
off = wdg.Button(help, text='Done', command=help.destroy)
off.grid(padx=24, pady=24, sticky='e')
ST.config_generic(help)
off.focus_set()
def attach_rt_clk_menu(self, evt):
self.widg = evt.widget
self.post(evt.x_root, evt.y_root)
for k,v in self.help_per_context.items():
if k == self.widg:
self.message = v[0]
self.help_title = v[1]
self.widg.update_idletasks()
def detect_text(self, evt):
'''
When the dropdown menu overlaps a second widget
that also responds to this evt, it can take an extra
brain clearing click to get this to work right.
'''
clikt = evt.widget
if (len(self.help_per_context[clikt][0]) == 0 and
len(self.help_per_context[clikt][1]) == 0):
self.disable_context_help()
else:
self.enable_context_help()
def disable_context_help(self):
self.entryconfigure('Context Help', state='disabled')
def enable_context_help(self):
self.entryconfigure('Context Help', state='normal')