Global Variable Not Recognized In JButton Function

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

Global Variable Not Recognized In JButton Function

Postby nakatsukasa_luke@bah.com » Wed Oct 19, 2022 2:06 pm

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
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)
nakatsukasa_luke@bah.com
Forum Newbie
Forum Newbie
 
Posts: 1
Posts Rating:0
Joined: Tue Oct 18, 2022 1:06 pm

Re: Global Variable Not Recognized In JButton Function

Postby james.boyd2@baesystems.com » Wed Nov 30, 2022 9:39 am

I am having the same issue and was wondering if a solution is available to the global variable not being recognized issue?
james.boyd2@baesystems.com
Forum Newbie
Forum Newbie
 
Posts: 1
Posts Rating:0
Joined: Thu Nov 03, 2022 7:25 am


Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 0 guests