挣扎着绑定文本文本小部件中的标签 [英] Struggling with binding on tags in tkiner text widget

查看:75
本文介绍了挣扎着绑定文本文本小部件中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在tkinter模块中的文本小部件上有点挣扎. 我添加了尝试将功能绑定到的标签.

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.

对原始帖子进行了修改,并删除了我输入的编码.在这里进行了与原始编码中相同的函数调用(这里不需要15k的代码):

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?

推荐答案

请考虑以下代码:

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)

您看到问题了吗?您需要将 callable 传递给绑定,并且您的函数没有返回可调用的内容.

Do you see the problem? You need to pass a callable to the binding, and your function isn't returning a callable.

解决方案是使用类似 lambda functools.partial .我更喜欢lambda主要是因为它不需要额外的导入.使用lambda,您需要该函数接受tkinter传递的event,并且还需要传递起始值.看起来像这样:

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

由于您要使用单个参数调用utskrift2,并且该参数是start值而不是event,因此您需要重新定义utskrift2使其看起来像这样:

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

这篇关于挣扎着绑定文本文本小部件中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆