Use a script to customise an image
This exercise describes how to use a script to update the Image displayed on a form:
- Click and drag an Image from the Toolbox to the Design Sheet:
- Click the Properties tab to display the properties of the Image control:
Rather than enter (or copy and paste) an image file path or URL into the Source property, you can set the source, and change other properties of the image, programmatically using a script.
When you open the Scripts pane, and a control is selected in the form, any functions associated with the properties of that control are shown in the Scripts pane. You can navigate the script using a list of functions at the top of the Scripts pane:
The following functions are used to update the image (and slogan) that is displayed, based on selections made elsewhere on the form:
from System.Windows.Media import Brushes # Global variables #sloganIndex = 1; slogalList =['https://images.mysafetysign.com/img/lg/K/thank-you-being-safe-sign-k-6981.png','https://images.mysafetysign.com/img/lg/S/think-safety-work-safely-reminder-sign-s2-1633.png','https://images.mysafetysign.com/img/lg/S/make-safety-your-priority-reminder-sign-s2-1611.png','https://images.mysafetysign.com/img/lg/S/three-causes-of-accidents-sign-s2-1643.png']; def UpdateSlogan (): sloganIndex = int(ImageLabel.Content) Console.Items.Add(ProjectData.SelectedIndex) if sloganIndex == 4: sloganIndex = 0 imgURL = slogalList[sloganIndex] SafetyImage.SetSource(imgURL) sloganIndex += 1 ImageLabel.Content= str(sloganIndex) def UpdateNavLabel (controlDataGrid, controlLabel): currentRow = controlDataGrid.SelectedIndex +1 rowCount = controlDataGrid.Items.Count controlLabel.Content = ('Row '+ str(currentRow) + ' of ' + str(rowCount) ) def Grid1_Loaded(sender, e): imgURL = slogalList[0] SafetyImage.SetSource(imgURL) UpdateNavLabel (ProjectData, ProjectNavLabel) def ProjectData_SelectionChanged(sender, e): UpdateNavLabel (ProjectData, ProjectNavLabel) i = ProjectData.SelectedIndex if i != int(ProjectIndexLabel.Content) and i>=0: UpdateSlogan () ProjectIndexLabel.Content = str(i) def CollarData_SelectionChanged(sender, e): UpdateNavLabel (CollarData, CollarNavLabel)
When you click Save your changes and click Run to run the form, the displayed image and slogan are updated as you make selections in the Project data grid: