Why does the interpreter expect my variable font to be global? (Solved)

I'm trying to implement style generation for vision projects by using datasets. I have a dataset that has foreground, background, bold, and italics values for 4 different urgency levels. Foreground and background are both colors, and bold and italics are both boolean. I can get the colors to apply, but when I try to update the font, I run into trouble. Here's what I'm trying:

def setStyle(component, errorLevel, styles = None):
	if not styles:
	  styles = system.tag.readBlocking("[default]styles")[0].value
	
	def getColumnByName(name):
		return styles.getColumnNames().index(name)
	
	def getStyleValue(name):
		return styles.getValueAt(errorLevel, getColumnByName(name))
		
	def getUpdatedFontValue(font):
		bold = getStyleValue("bold") * font.BOLD
    	italic = getStyleValue("italics") * font.ITALIC
    	return font.derive(bold | italic)

	component.foreground = getStyleValue("foreground")
	component.background = getStyleValue("background")
	component.font = getUpdatedFontValue(component.font)

When I run this code, I get the following error:

Traceback (most recent call last):
  File "<event:propertyChange>", line 1, in <module>
  File "<module:setStyle>", line 13, in setStyle
NameError: global name 'font' is not defined

I think font should be defined in the function definition, what am I doing wrong?

I figured out that the problem was that I had a mix of spaces and tabs.

All other things aside (I would strongly recommend the use of the style customizer over scripting everything) -- this is a really bad idea. propertyChange events are fired constantly, and on the event dispatch thread. You should be using a tag binding (guaranteed asynchronous, off the EDT) to bring in your dataset of styles.

I understand. I'm trying to learn how to do scripting, and for me, it's useful to have problems to solve, so I invent them. In this case, the problem I invented is that I have a user who is colorblind, so they need a different color palette when they're logged in.

1 Like