努力在 tkiner 文本小部件中绑定标签 [英] Struggling with binding on tags in tkiner text widget

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

问题描述

我在 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] + "
"
               self.tekstfelt.insert(pos, setning)
               setning.replace("
","")
               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.

解决方案是使用类似 lambdafunctools.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")

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

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