## Action in Button to run script

Discussions on extending SharpCap using the built in Python scripting functionality
JesusRL
Posts: 11
Joined: Wed Aug 05, 2020 4:36 pm

### Action in Button to run script

Hi,

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

T&R

Posts: 5150
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

### Re: Action in Button to run script

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

JesusRL
Posts: 11
Joined: Wed Aug 05, 2020 4:36 pm

### Re: Action in Button to run script

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

Posts: 5150
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

### Re: Action in Button to run script

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

JesusRL
Posts: 11
Joined: Wed Aug 05, 2020 4:36 pm

### Re: Action in Button to run script

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()



JesusRL
Posts: 11
Joined: Wed Aug 05, 2020 4:36 pm

### Re: Action in Button to run script

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

Posts: 5150
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

### Re: Action in Button to run script

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