Setting Label Font Through Scripting

So I’m trying to set the font of a label when the screen the label on is shown. I have gone around into a few places but it doesn’t seem to work. I currently have this script running on the container that holds the label.

from java.awt import Font
if event.propertyName == "componentRunning" and event.newValue:
	print event.source.getComponent('bannerText').font
	event.source.getComponent('bannerText').font = Font('Arial', Font.BOLD, 50)
	print event.source.getComponent('bannerText').font

The print statements appear to act correctly…

java.awt.Font[family=Arial,name=Arial,style=bold,size=20]
java.awt.Font[family=Arial,name=Arial,style=bold,size=50]

Why not just set the font property?

This is a simplified example. We are going to be passing each label into a script to initialize it so they all look the same. Also that way if we need to change it, we can change it in 1 location for every label without having to go to each screen. We are doing the same thing with tables. Each table gets passed to a script that sets up all the aspects so they are all the same.

In the a project startup script, create a font entry in globals:

from java.awt import Font
globalFont = Font('Arial', Font.BOLD, 50)
system.util.getGlobals()['globalFont'] = globalFont

In the propertyChange script for the label:

# The 'text' property fires every time a screen starts
if event.propertyName == 'text':
	# Get component object and global font
	component = event.source
	globalFont = system.util.getGlobals()['globalFont']
	# set component to global font, if they don't match
	if component.font != globalFont:
		component.font = globalFont
		

After you get the desired effects you want, you can put this label into a custom component pallete and use it wherever you wish.

That doesn’t work either. I can see from the print statements that the font is taking, but the screen doesn’t update. It’s like you need to invoke a repaint of the screen.

What version are you using? I tested this under 8.0.9. :confused:

8.0.7

Are you testing in the designer only? What about a ‘real’ client? I strongly suspect something else is at play here.

I had tested it from the stand alone client launcher and from the client launched from the designer and by putting the designer into preview mode. None of them were working. We just so happen to have to reboot the dev system today and it now works in all 3 cases.

And just like that the problem is back. Now it will only work when i put the designer into Preview mode. Launching the client in any way causes it not to work.

This is now on the label component

if event.propertyName == 'text':
	print event.source.font
	print theme.label.pageTitle()
	event.source.font = theme.label.pageTitle()
	print event.source.font

Here is the script “theme.label.pageTitle()”

from java.awt import Font
def pageTitle():
	return Font('Arial', Font.BOLD, 50)

Here is the console output

java.awt.Font[family=Arial,name=Arial,style=bold,size=23]
java.awt.Font[family=Arial,name=Arial,style=bold,size=50]
java.awt.Font[family=Arial,name=Arial,style=bold,size=50]

This is the complete console when launched from Client Launcher

19:41:01.248 [Thread-0] INFO com.inductiveautomation.ignition.client.gateway_interface.GatewayConnectionManager - Updated login state. Logged in? true, Username: admin, Roles: [Administrator], Security Zones: null
Creating frame. extendedState=6, bounds=java.awt.Rectangle[x=0,y=0,width=1918,height=938]
19:41:01.355 [Thread-4] INFO tags.manager.gwinterface - Tag poll rate changed to 250 ms
19:41:01.376 [ClientExecEngine-2] INFO tags.subscriptions - Changing connected quality to 'Good'
19:41:03.159 [Thread-4] INFO com.inductiveautomation.reporting.client.ReportingClientHook - Starting up Reporting Module. Mode: Trial
19:41:03.366 [Thread-4] ERROR Scripting.ScriptManager.OMITTED- Warning: collision at system.util.initialize
19:41:03.753 [AWT-EventQueue-0] INFO vision.App - Starting Up...
java.awt.Font[family=Arial,name=Arial,style=bold,size=23]
java.awt.Font[family=Arial,name=Arial,style=bold,size=50]
java.awt.Font[family=Arial,name=Arial,style=bold,size=50]

You’ve tried this script on a brand new window, no bindings, other scripts, etc?
What if you use ‘Dialog’ instead of ‘Arial’? What if you change the script where you’re assigning; use:

	newFont = theme.label.pageTitle()
        print newFont
	event.source.setFont(newFont)

I tried it using the setFont method and that appears to make it work 100% of the time now. Is there an issue when just setting it equal to a font as I was doing verse using the setFont method? I would have thought they would produce the exact same result.

They should, but it is a little bit of Jython “magic”; Java code style discourages publicly accessible properties, and instead encourages the ‘Bean’ pattern, where there’s a private field and a getX()/setX() method. Jython does some under-the-hood stuff to turn getX/setX into an accessible “property” x - so your code, assigning component.font = something is technically calling component.setFont(something); or at least, it should be.

It’s not inconceivable that this breaks down somewhere; either because of something about Ignition’s components, or the way we call Jython code, etc.

1 Like

Simple enough. Thanks for the explanation. I figured since the property was exposed that I should use that instead of the get/set idea that I’m used to in Java. If I encounter weird bugs, I’ll know to try the getter and setter first and go from there.