2013-05-27 15 views
0

我試圖運行使用的Jython Python腳本,Jython的錯誤 「異常線程 」AWT-EventQueue的-0「」

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

但它顯示錯誤:

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

在這裏,我沒有任何參數傳遞給動作函數!

我在哪裏出錯?

謝謝

回答

1

按鈕按鈕總是傳遞一個事件。你設置爲JButton.actionPerformed的任何東西都需要處理它才能正常工作。

試試這個:一旦你意識到這一點

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

這是非常有用的。假設你有兩個按鈕綁定到相同的事件,你需要知道哪一個被按下。

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

另外,您也不允許向actionPerformed方法發送多個事件。如果你想要你可以看看workaround in this question

相關問題