Jython - attempting to call functions from JFrame, receiving 'NoneType' error
So I'm playing around with Jython, trying to slap together a generic GUI. Nothing beyond what they have on the Jython Wiki for swing examples. So I declare a JFrame, and then try to add a panel, some text fields, all that good stuff. I get this error when I run it, however. "'NoneType' object has no attribute 'add'"
Here's the basic code I have.
from javax.swing import *
frame = JFrame('E-mail Gathering', defaultCloseOperation = JFrame.EXIT_ON_CLOSE, size =(600,400), locationRelativeTo = None).setVisible(True)
pnl = JPanel()
frame.add(pnl)
self.textfield1 = JTextField('username:',15)
pnl.add(self.textfield1)
self.textfield2 = JTextField('password:', 15)
pnl.add(self.textfield2)
mailButton = JButton('Login',actionPerformed=self.checkmail)
pnl.add(mailButton)开发者_如何学Python
frame.pack()
frame.setVisible(True)
I know the relevant part where it's crashing is at 'frame.add(pnl)' with the aforementioned error. I figured I'd throw the rest up there just in case I'm making some even greater mistakes. I feel like something's wrong where I'm not declaring frame as a JFrame properly, but I know that's not the case because it creates the frame just fine if I don't try to add anything to it.
Thanks for any advice or suggestions you have.
In this line:
frame = JFrame('E-mail Gathering', defaultCloseOperation = JFrame.EXIT_ON_CLOSE, size =(600,400), locationRelativeTo = None).setVisible(True)
you are creating a JFrame
, calling its setVisible
method, and assigning the return value of setVisible
to frame
. setVisible
doesn't return a value, so frame
is None
. This causes frame.add
to fail.
As you call setVisible
at the end anyway, and because you probably don't want to make the frame visible before you have added other components to it and called pack
, just remove the setVisible
call:
frame = JFrame('E-mail Gathering', defaultCloseOperation = JFrame.EXIT_ON_CLOSE, size =(600,400), locationRelativeTo = None)
精彩评论