Global Variable Not Recognized In JButton Function

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

Global Variable Not Recognized In JButton Function

Postby » 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

test = "this is a test"

frame = JFrame("Hello")

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
    loggy = Application.getInstance().GUILog

    loggy.log(test)     # error occurs at this line because doesn't recognize it as global variable


btn = JButton("Add", actionPerformed = add)
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 » 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?
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