Page 1 of 1

Action in Button to run script

Posted: Thu Nov 19, 2020 10:23 am
by JesusRL
Hi,

What would be the Action parameter to configure a CustomButton to run a sepecific script?

T&R

Re: Action in Button to run script

Posted: Thu Nov 19, 2020 7:22 pm
by admin
Hi,

you can just pass the function that you want the button to invoke that parameter – see this post for a simple example

viewtopic.php?t=169#p712

Cheers, Robin

Re: Action in Button to run script

Posted: Thu Nov 19, 2020 8:12 pm
by JesusRL
Thank you Robin, I had already tested that and works perfectly ... but I want to run a py script and I dont find the action to invoke this.
T&R

Re: Action in Button to run script

Posted: Fri Nov 20, 2020 9:39 pm
by admin
Hi,

I'm afraid I'm not understanding quite what you're trying to achieve then. Do you have a separate .py file that you want to run code from? If you have a file called x.py in c:\src, then I think you can do

Code: Select all

sys.path.append('c:\src')
import x

# now you can call functions from x.py
(note the syntax for the above may be incorrect, but hopefully the idea is correct).

Cheers, Robin

Re: Action in Button to run script

Posted: Sat Nov 21, 2020 12:04 pm
by JesusRL
Thank you again for your support (and patiente)

I have re-read my request and it was not clear at all. I have already solved my issue but I will explain it here just in case someone else needs it.

I had a couple of scripts that run as forms; I use them very often so I wanted to have a button instead of searching in menus and folders to run them (lazyness)

The way they normally ran was:

Code: Select all

form = MoveToAltAz()
Application.Run(form)
Now, in order to have a button for them I have substituted this by:

Code: Select all

form = MoveToAltAz()

def show_script():
    form.ShowDialog()

SharpCap.AddCustomButton("GOTO AltAz", None, None, show_script)

Re: Action in Button to run script

Posted: Tue Nov 24, 2020 4:19 pm
by JesusRL
Just to make it a bit more elegant ... how can I associate an image to the icon?

A file address does not work

Code: Select all

SharpCap.AddCustomButton("GOTO AltAz", '..\image.png', None, show_script)
T&R

Re: Action in Button to run script

Posted: Tue Nov 24, 2020 7:44 pm
by admin
Hi,

you need to pass a .NET System.Drawing.Bitmap object, which does have a constructor that takes a file path to an image file.

Code: Select all

clr.AddReference("System.Drawing")
from System.Drawing import Bitmap
image = Bitmap("..\\image.png")
Hope this helps, Robin