Action in Button to run script

Discussions on extending SharpCap using the built in Python scripting functionality
Post Reply
JesusRL
Posts: 70
Joined: Wed Aug 05, 2020 4:36 pm
Location: Madrid

Action in Button to run script

#1

Post by JesusRL »

Hi,

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

T&R
User avatar
admin
Site Admin
Posts: 13177
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Action in Button to run script

#2

Post 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
JesusRL
Posts: 70
Joined: Wed Aug 05, 2020 4:36 pm
Location: Madrid

Re: Action in Button to run script

#3

Post 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
User avatar
admin
Site Admin
Posts: 13177
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Action in Button to run script

#4

Post 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
JesusRL
Posts: 70
Joined: Wed Aug 05, 2020 4:36 pm
Location: Madrid

Re: Action in Button to run script

#5

Post 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)
JesusRL
Posts: 70
Joined: Wed Aug 05, 2020 4:36 pm
Location: Madrid

Re: Action in Button to run script

#6

Post 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
User avatar
admin
Site Admin
Posts: 13177
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Action in Button to run script

#7

Post 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
Post Reply