Pass additional value with .Click event

Is there a way to pass a value with a button click?
I have a lot of buttons (17) in my form, and all I need them to give me is a specific value.

self.WoodButton = forms.Button(Text = 'Wood')
self.WoodButton.Click += self.OnWoodButtonClick

its starting to look silty when all that def is doing is this

# Atr button click handler
def OnWoodButtonClick(self, sender, e):
atrClass = '000'
self.AssignAttributes(atrClass)

Away to have something like self.OnWoodButtonClick(000)?
if so, what would it need to look like on the def end as well as on the .Click end, so it doesn’t error out.
Thanks in advance!

@mikhail,

in your OnWoodButtonClick function, you have an argument for the sender which can give you the button which invoked the function. Maybe you can do something like this:

def OnWoodButtonClick(self, sender, e):
    if sender.Text == "Wood":
        self.AssignAttributes("000")

does that help ?
_
c.

Btw, you can also add a Tag to a control in your __init__ function and get this from the sender:

import Eto
import Rhino

class Form1(Eto.Forms.Dialog[bool]):
    def __init__(self):
        self.WoodButton = Eto.Forms.Button(Text="Wood", Tag="000")
        self.WoodButton.Click += self.OnButtonClick
        self.MetalButton = Eto.Forms.Button(Text="Metal", Tag="001")
        self.MetalButton.Click += self.OnButtonClick
        layout = Eto.Forms.DynamicLayout()
        layout.AddRow(self.WoodButton)
        layout.AddRow(self.MetalButton)
        self.Content = layout
        self.Title = "Title"

    def OnButtonClick(self, sender, e):
        if not isinstance(sender, Eto.Forms.Button): return
        print "Text={} Tag={}".format(sender.Text, sender.Tag)

dialog = Form1()
rc = dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)
dialog.Dispose()

_
c.

4 Likes

@clement
Thank toy for your help!
Super useful to know about the sender.Text, could come in handy at some point.
Used the Tag option, was able to kill 70 lines of code that way, and a lot less if statements, as I can pass that value right into the function. :slightly_smiling_face:

Noob question: how do you pass sender.Tag value further to the main function? I know about .03% about anything Eto.

Hi @Asterisk,

since the dialog is modal, you only get back to main function if it is closed. You could just return the sender.Tag as dialog result like so:

import Eto
import Rhino

class Form1(Eto.Forms.Dialog[str]):
    def __init__(self):
        self.WoodButton = Eto.Forms.Button(Text="Wood", Tag="000")
        self.WoodButton.Click += self.OnButtonClick
        self.MetalButton = Eto.Forms.Button(Text="Metal", Tag="001")
        self.MetalButton.Click += self.OnButtonClick
        layout = Eto.Forms.DynamicLayout()
        layout.AddRow(self.WoodButton)
        layout.AddRow(self.MetalButton)
        self.Content = layout
        self.Title = "Title"

    def OnButtonClick(self, sender, e):
        if not isinstance(sender, Eto.Forms.Button): return
        self.Close(sender.Tag.ToString())
    
dialog = Form1()
rc = dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)
print rc

dialog.Dispose()

Note that this was tested under Windows only.

_
c.

1 Like

Ok, cool, I’ll just use sc.doc.Strings.SetString() then. Thanks!

This keeps on helpin noobs like me even 3 years later!

1 Like