0

i tried to run a python script using Jython,

from javax.swing import JButton,JFrame
def action():
    execfile(r"E:\stack.py")
frame = JFrame("window")
button = JButton("button", actionPerformed = action)
frame.add(button)
frame.show()

But it's showing error:

Exception in thread "AWT-EventQueue-0" TypeError: action() takes no arguments (1 given)

Here i'm not passing any arguments to action function!

where am i going wrong?

Thank you

user19911303
  • 449
  • 2
  • 9
  • 34

1 Answers1

1

The button press always passes an event. Anything you set to JButton.actionPerformed needs to handle it to work properly.

Try this:

from javax.swing import JButton,JFrame
def action(event):
    execfile(r"E:\stack.py")
frame = JFrame("window")
button = JButton("button", actionPerformed = action)
frame.add(button)
frame.show()

This can be quite useful once you're aware of it. Suppose you have two buttons bound to the same event and you need to know which one was pressed.

def show_which_button_was_pressed(event):
   sender = event.getSource()
   print sender.getText()

As an aside, you're also not allowed to send more than event to an actionPerformed method. If you were to want to you can look at a workaround in this question.

Community
  • 1
  • 1
sarwar
  • 2,835
  • 1
  • 26
  • 30