[英]AttributeError: 'Event' object has no attribute 'Text1'
My purpose is to get the mouse selected text through the Tkinter Text
control.我的目的是通过Tkinter Text
控件获取鼠标选中的文本。
Part of the code:部分代码:
self.Text1 = Text(top)
self.Text1.place(relx=0.07, rely=0.09, relheight=0.04, relwidth=0.34)
self.Text1.configure(background="white")
self.Text1.configure(font="TkTextFont")
self.Text1.configure(foreground="black")
self.Text1.configure(highlightbackground="#d9d9d9")
self.Text1.configure(highlightcolor="black")
self.Text1.configure(insertbackground="black")
self.Text1.configure(selectbackground="#c4c4c4")
self.Text1.configure(selectforeground="black")
self.Text1.configure(width=294)
self.Text1.configure(wrap=WORD)
self.Scrolledtext1 = ScrolledText(top)
self.Scrolledtext1.place(relx=0.46, rely=0.19, relheight=0.62
, relwidth=0.4)
self.Scrolledtext1.configure(background="white")
self.Scrolledtext1.configure(font="TkTextFont")
self.Scrolledtext1.configure(foreground="black")
self.Scrolledtext1.configure(highlightbackground="#d9d9d9")
self.Scrolledtext1.configure(highlightcolor="black")
self.Scrolledtext1.configure(insertbackground="black")
def button_down(self,):
global s
s = self.Text1.index('@%s,%s wordstart' % (event.x, event.y))
def button_up(self, ):
global e
e = self.Text1.index('@%s,%s wordend' % (event.x, event.y))
def test(self,):
print(self.Scrolledtext1.get(s,e))
self.Scrolledtext1.bind("<Button-1>", button_down)
self.Scrolledtext1.bind("<ButtonRelease-1>", button_up)
self.Button2.configure(command=test(self,))
Exception in Tkinter callback: Tkinter回调异常:
screenshot of traceback showing exception being generated显示生成异常的回溯屏幕截图
AttributeError: 'Event' object has no attribute 'Text1'
Your button_up
, button_down
and test
functions are not bound methods. 您的button_up
, button_down
和test
函数不是绑定方法。 Therefore, they do not get the self
parameter. 因此,它们没有获得self
参数。 The value being passed as self
is actually an Event object. 作为self
传递的值实际上是一个Event对象。 Since you use self
in the functions, I would recommend changing them to being bound methods. 由于您在函数中使用self
,因此建议将它们更改为绑定方法。 I assume the code you posted is in the __init__
method. 我假设您发布的代码在__init__
方法中。 If so, change the last three lines to: 如果是这样,请将最后三行更改为:
self.Scrolledtext1.bind("<Button-1>", self.button_down)
self.Scrolledtext1.bind("<ButtonRelease-1>", self.button_up)
self.Button2.configure(command=self.test)
And move the three functions to be members of the class: 并将三个函数移为该类的成员:
def button_down(self, event):
global s
s = self.Text1.index('@%s,%s wordstart' % (event.x, event.y))
def button_up(self, event):
global e
e = self.Text1.index('@%s,%s wordend' % (event.x, event.y))
def test(self, event):
print(self.Scrolledtext1.get(s,e))
This is how your class should look afterwards. 这是您班级以后的样子。 The three event handler functions are all inside the class. 这三个事件处理函数都在类内部。
class YourClassName:
def __init__(self, *args, **kwargs):
# initialization code
# ...
self.Scrolledtext1.bind("<Button-1>", self.button_down)
self.Scrolledtext1.bind("<ButtonRelease-1>", self.button_up)
self.Button2.configure(command=self.test)
def button_down(self, event):
global s
s = self.Text1.index('@%s,%s wordstart' % (event.x, event.y))
def button_up(self, event):
global e
e = self.Text1.index('@%s,%s wordend' % (event.x, event.y))
def test(self, event):
print(self.Scrolledtext1.get(s,e))
Try to pass evt instead of self in your function尝试在 function 中传递 evt 而不是 self
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.