.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)