JLabel 上的 Jython 鼠标侦听器导致 TypeError
我正在 Jython 中创建一个应用程序,尝试捕获 JLabel 上的鼠标事件。我有一个包含 JLabel 的 JFrame,但是当我尝试向其中添加鼠标侦听器时,我得到:
TypeError: write only attribute
主窗口:
class Commander(JFrame):
...
self.image = ImageIcon()
self.label = JLabel(self.image)
self.mouseListener = ScreenMouseListener()
self.label.addMouseListener(self.mouseListener) <- This line causes the TypeError
...
鼠标侦听器扩展了 MouseAdapter:
class ScreenMouseListener(MouseAdapter):
def mousePressed(self, event):
print "Mouse pressed"
搜索错误导致我 PyBeanEvent,但我不明白为什么会发生。哪个属性是只写的?
编辑:删除导致错误的行后,我注意到 mousePressed 函数被调用! “mouseListener”可能是 JFrame 的属性吗?
I'm making an application in Jython where I try to capture mouse events on a JLabel. I have a JFrame that contains a JLabel, but when I try to add a mouse listener to it I get:
TypeError: write only attribute
The main window:
class Commander(JFrame):
...
self.image = ImageIcon()
self.label = JLabel(self.image)
self.mouseListener = ScreenMouseListener()
self.label.addMouseListener(self.mouseListener) <- This line causes the TypeError
...
The mouse listener extends MouseAdapter:
class ScreenMouseListener(MouseAdapter):
def mousePressed(self, event):
print "Mouse pressed"
Searching for the error leads me to PyBeanEvent, but I don't understand why it happens. Which attribute is write-only?
Edit: After removing the line that caused the error, I noticed that the mousePressed function gets called! Is "mouseListener" perhaps a property of JFrame?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信JFrame.mouseListener是一个只写属性,即没有实现get函数,所以当它传递给self.label.addMouseListener时,它无法被读取。
通过执行以下操作解决了该问题:
I believe JFrame.mouseListener is a write-only property, i.e. there is no get function implemented, so when it was passed to self.label.addMouseListener, it could not be read.
Solved it by doing the following instead: