Script work in script console but not work from from button perspective

i am using following code in script console, script work in console fine, but not work
from button (basically its user Guide for operation)
'''

Import necessary modules

from java.awt import Color
from javax.swing import DefaultListModel, Timer
from javax.swing import JList
from javax.swing import JScrollPane
from javax.swing import JFrame
from java.awt import BorderLayout

Create a list model to store messages

messageListModel = DefaultListModel()

Function to add a message to the list

def addMessage(message):
messageListModel.addElement(message)

Function to clear all messages from the list

def clearMessages():
messageListModel.clear()

Create a JFrame to display the list

frame = JFrame("UGU")
frame.setSize(400, 300)
#frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

Create a JList with the message list model

messageList = JList(messageListModel)

Set some visual properties for the list

messageList.setBackground(Color.WHITE)
messageList.setForeground(Color.BLACK)

Create a scroll pane to hold the list

scrollPane = JScrollPane(messageList)

Add the scroll pane to the frame

frame.getContentPane().add(scrollPane, BorderLayout.CENTER)

Function to display the frame

def showFrame():
frame.setVisible(True)

Function to close the frame after 3 seconds

def closeFrame():
frame.dispose()

value1=True
value2=True
value3=False

Example usage:

if value1:
addMessage("Error: Pump 1 failure")
if value2:
addMessage("Warning: Tank level low")
if value3:
addMessage("Error: Pump 2 failure")

Call the function to display the frame

showFrame()

Schedule the frame to close after 3 seconds

timer = Timer(2000, lambda e: closeFrame())
timer.setRepeats(False)
timer.start()
'''

Can you post your code formatted without the comments you added?

Just post exactly what's in your script console. Use something like
image

and code will show up formatted, like this:

def sampleFunction(prob):
    print "hello"

Also, I don't really understand what you're trying to do... You tagged the post as Perspective, but you're using Java Swing? For what reason?

All the scripting you write on Perspective buttons or other components will run on the Ignition server, so I'm not sure what you're expecting to happen.

ChatGPT (or whatever LLM you're using) doesn't understand Ignition well enough to be helpful. Don't bother asking it Ignition related questions.

You may get a volunteer well motivated enough to untangle your intentions from what you've posted, but what you've done here has nothing to do with Ignition's provided visualization systems (Vision or Perspective).

Please see Wiki - how to post code on this forum. Then hit the pencil icon under your post to edit and fix it. Thanks.