Global Variable Not Recognized In JButton Function

I'm having difficulty finding a way to provide arguments or global variables to a function performed by clicking a JButton. My end goal is to have a check box in the GUI set a boolean that is used in the actionPerformed call (add()) but I couldn't find a way to do that. Below is a simplified example code demonstrating the problem (not code of my final solution just to demonstrate the type of error i'm encountering). I trying to use a global variable called "test" (string) to get logged in the Notification Window when MagicDraw runs the macro. Instead, when running the code below the following error occurs
Note that the global variable is recognized for the function doThis() but is not recognized when the JButton executes add(). Why is this and how can I provide a variable to be used by add() either as a global or an argument?
Appreciate any help!
.NameError: global name 'test' is not defined
Note that the global variable is recognized for the function doThis() but is not recognized when the JButton executes add(). Why is this and how can I provide a variable to be used by add() either as a global or an argument?
Appreciate any help!
- Code: Select all
from javax.swing import JFrame, JLabel, JButton, JTextField
from com.nomagic.magicdraw.core import Application
loggy = Application.getInstance().GUILog
loggy.clearLog()
test = "this is a test"
frame = JFrame("Hello")
frame.setLocation(100,100)
frame.setSize(300,200)
frame.setLayout(None)
def doThis():
loggy.log("do this. "+test) # this function works where "test" is recognized as global variable
def add(event):
from com.nomagic.magicdraw.core import Application
from com.nomagic.magicdraw.openapi.uml import SessionManager
SessionManager.getInstance().createSession("Test")
loggy = Application.getInstance().GUILog
loggy.log("add")
loggy.log(test) # error occurs at this line because doesn't recognize it as global variable
doThis()
btn = JButton("Add", actionPerformed = add)
btn.setBounds(60,80,60,20)
btn.setVisible(True)
frame.add(btn)
frame.setVisible(True)