What is the “best” way to handle/update the checked state of a menu item in python? For example, if you have multiple items in your dropdown menu, how can I display the check next to the item the user selects?
try:
item1 = Grasshopper.Kernel.GH_Component.Menu_AppendGenericMenuItem(
menu, "MenuItem 1", self.OnClicked, None, None, True, False);
item1.Name = "Item 1";
item2 = Grasshopper.Kernel.GH_Component.Menu_AppendGenericMenuItem(
menu, "MenuItem 2", self.OnClicked, None, None, True, False);
item2.Name = "Item 2";
item3 = Grasshopper.Kernel.GH_Component.Menu_AppendGenericMenuItem(
menu, "MenuItem 3", self.OnClicked, None, None, True, False);
item3.Name = "Item 3";
except Exception, ex:
System.Windows.Forms.MessageBox.Show(str(ex))
def OnClicked(self, sender, args):
try:
sender.Checked = True #how to get this "back" up to menuitem?
System.Windows.Forms.MessageBox.Show("My name is {}...".format(sender.Name))
System.Windows.Forms.MessageBox.Show("I am {}...".format(sender.Checked))
self.ExpireSolution(True)
except Exception, ex:
System.Windows.Forms.MessageBox.Show(str(ex))
I am not sure what part is really giving trouble, because the original sample gave a very similar example with “checked”. The way is to store an attribute for each independent variable you want, then read it when menu items are appended.
Thanks! I was missing the step of storing an attribute for each independent variable and doing something in the event handler to change the menu items from checked to unchecked. Your samples allowed me to get my first custom context menu working! It’s good enough for what I was hoping to accomplish. Thank you again for all of your help!
Dear Chris Hanley, with this method, is there a way to save the user’s selection in a gh/ghx file, next time when user open the file, it’s last chosen option rather than the default one.
I would guess you would want to look at something along the lines of using pickle or shelve to save a selected variable, (assuming you are using python). I don’t have any examples off hand, but that would be where I would start.
Thank you, the question is not which way to save and read the settings, any object and string conversion is possible.It is how to save the settings to the gh file, and read the settings from a specific component instance when user open the gh file.
Has anybody tried sth similar in Rhino 8/python 3? or suspects why it wouldn’t work?
from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import Rhino
import rhinoscriptsyntax as rs
class Cntxt(component):
def __init__(self):
super(Cntxt, self).__init__()
self.enabled = False
def RunScript(self):
a = self.enabled
return a
def OnTextMenuClick(self, sender, args):
try:
# Toggle the enabled state and display a message box
self.enabled = not self.enabled
System.Windows.Forms.MessageBox.Show(f"I am now {self.enabled}...")
# Expire the solution so the component re-runs
self.ExpireSolution(True)
except Exception as ex:
System.Windows.Forms.MessageBox.Show(str(ex))
def AppendAdditionalMenuItems(self, items):
# Call the base class method to add the default menu items
super(Cntxt, self).AppendAdditionalMenuItems(items)
try:
# Add a custom menu item to toggle the enabled state
image = None
item = items.Items.Add(f"Additional state to: {not self.enabled}", image, self.OnTextMenuClick)
item.Checked = self.enabled
except Exception as ex:
System.Windows.Forms.MessageBox.Show(str(ex))