问题
I'm struggling a bit with the text widget in the tkinter module. I have added tags that I try to bind a function to.
Regardless of how I type it, it happens one of two things. Either I can click in the text widget but the only function popping up is for the last item regardless of where i click. Number two that happens is that it just spews out all functions automatically.
Did an edit on the original post and removed the coding i've typed. Have made the same function calls here as I had in the original coding (less 15k of code that's unecessary here):
#!/usr/bin/en python
# *-* coding: utf-8 *-*
import platform as platform
from tkinter import *
class guidelen:
def __init__(self, master):
self.master = master
self.master.title("programmet")
self.master.geometry("400x400")
self.populate()
def populate(self):
self.meny = Menu(self.master, tearoff=0)
self.startmeny = Menu(self.meny, tearoff=0)
self.startmeny.add_command(label="Avslutt",command=self.master.quit)
self.meny.add_cascade(label="Start", menu=self.startmeny)
self.master.config(menu=self.meny)
self.tekstfelt = Text(self.master)
self.tekstfelt.pack(fill=BOTH, expand=True)
setninger = ["første setningen","andre setningen","tredje setningen"]
start = 0
posisjon = 1
while start < len(setninger):
pos = str(posisjon) + ".0"
b = len(setninger[start])
pos2 = str(posisjon) +"."+ str(b)
setning = setninger[start] + "\n"
self.tekstfelt.insert(pos, setning)
setning.replace("\n","")
self.tekstfelt.tag_add(setning, pos, pos2)
self.tekstfelt.tag_bind(setning, "<Button-1>", self.utskrift2(start))
print(start)
posisjon += 1
start += 1
def utskrift(self, event):
print("Prøver tag bindingen")
def utskrift2(self, event):
if event == 0:
print("Taggen til første linjen")
if event == 1:
print("Taggen til andre linjen")
if event == 2:
print("Taggen til tredje linjen")
if __name__ == "__main__":
start = Tk()
guidelen(start)
start.mainloop()
so regardless of how i alter the function, it always gives the same bind on all items in the text widget or it just spews out all bindings/function calls automatically.
Is there any out there that knows what I'm doing wrong?
回答1:
Consider this code:
self.tekstfelt.tag_bind(setning, "<Button-1>", self.utskrift2(start))
It has the same behavior as this code:
result = self.utskrift2(start)
self.tekstfelt.tag_bind(setning, "<Button-1>", result)
Do you see the problem? You need to pass a callable to the binding, and your function isn't returning a callable.
The solution is to use something like lambda or functools.partial. I prefer lambda
mainly because it doesn't require an extra import. Using lambda
, you need for the function to accept the event
passed by tkinter, and you also need to pass in the start value. It would look something like this:
self.tekstfelt.tag_bind(setning, "<Button-1>", lambda event, start=start: self.utskrift2(start))
Since you are calling utskrift2
with a single argument, and that argument is the start
value rather than the event
, you need to redefine utskrift2
to look like this:
def utskrift2(self, start):
if start == 0:
print("Taggen til første linjen")
if start == 1:
print("Taggen til andre linjen")
if start == 2:
print("Taggen til tredje linjen")
来源:https://stackoverflow.com/questions/62392080/struggling-with-binding-on-tags-in-tkiner-text-widget