Action in Button to run script

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

Action in Button to run script

Post by JesusRL » Thu Nov 19, 2020 10:23 am

Hi,

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

T&R

User avatar
admin
Site Admin
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

Post by admin » Thu Nov 19, 2020 7:22 pm

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

Post by JesusRL » Thu Nov 19, 2020 8:12 pm

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: 5150
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Action in Button to run script

Post by admin » Fri Nov 20, 2020 9:39 pm

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

Post by JesusRL » Sat Nov 21, 2020 12:04 pm

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: 11
Joined: Wed Aug 05, 2020 4:36 pm

Re: Action in Button to run script

Post by JesusRL » Tue Nov 24, 2020 4:19 pm

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: 5150
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Action in Button to run script

Post by admin » Tue Nov 24, 2020 7:44 pm

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