Tkinter索引单词问题 [英] Tkinter indexing words issue

查看:238
本文介绍了Tkinter索引单词问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在python 2.7中编写一个Tkinter应用程序。我正在使用wordstart和wordend索引来获取被单击的单词。这适用于常规单词,但它不适用于带连字符的单词。

I'm writing a Tkinter application in python 2.7. I am using wordstart and wordend indexes to get the word that was clicked on. This works perfectly fine with regular words, but it does not work with hyphenated words.

这是我的代码:

from Tkinter import *

master = Tk()

def userOptions(event, clickposition):
    index1 = clickposition + " wordstart"
    index2 = clickposition + " wordend"
    user = text.get(index1, index2)
    print user
    return

def userEnter(event):
    text.config(cursor="hand2")
    return

def userLeave(event):
    text.config(cursor="arrow")
    return  

text = Text(master)
text.insert(INSERT, "This is a sentence\n")
text.insert(INSERT, "This is a sentence with dashes-between some-words\n")
text.pack()
text.tag_add("click", "1.0", "end")
text.tag_bind("click", "<Enter>", userEnter)
text.tag_bind("click", "<Leave>", userLeave)
text.tag_bind("click", "<Button-1>", lambda event: userOptions(event, text.index("@%d,%d" % (event.x, event.y))))
text.config(state = DISABLED)

master.mainloop()

如何对此进行配置,以便打印用户可以打印整个带连字符的单词而无需拆分连字符?例如,根据您点击的位置打印字符串破折号而不是破折号,介于或 - 。

How do I configure this so print user can print the whole hyphenated word without splitting at the hyphen? For example, printing the string "dashes-between" instead of "dashes", "between", or "-" depending on where you click.

推荐答案

你无法修改wordstart定义单词的方式。来自官方文档:

You can't modify how "wordstart" defines "words". From the official documentation:


?submodifier? wordstart - 调整索引以引用包含当前索引的单词的第一个
字符。一个单词包含
任意数量的相邻字符,即字母,数字或
下划线,或单个字符不是其中之一。如果给出了
显示子修饰符,则仅检查非省略的
字符,否则将检查所有字符(是否已删除)。

?submodifier? wordstart - Adjust the index to refer to the first character of the word containing the current index. A word consists of any number of adjacent characters that are letters, digits, or underscores, or a single character that is not one of these. If the display submodifier is given, this only examines non-elided characters, otherwise all characters (elided or not) are examined.

您可以使用文本小部件的内置搜索功能查找单词的开头和结尾,但是您要定义单词。您可以搜索正则表达式,这样就可以搜索 [ - \w] 之类的模式来获取短划线或单词字符。

You can use the built-in search feature of the text widget to find the start and end of the word for however you want to define "word". You can search for regular expressions, so you can search for a pattern like [-\w] to get either a dash or a word character.

在我的头顶,它可能看起来像这样:

Off the top of my head, it might look something like this:

def userOptions(event):
    count = IntVar()
    pattern = r'[-\w]+'

    # find the beginning of the "word", starting _after_
    # the character clicked on
    start = "@%d,%d +1c" % (event.x, event.y)
    index1 = text.search(pattern, start, backwards=True, regexp=True)

    # starting with the beginning, find the end and save
    # the number of characters that matched.
    text.search(pattern, index1, regexp=True, count=count)

    # compute the ending index of the match
    index2=text.index("%s + %s c" % (index1, count.get()))

    # get the text
    user = text.get(index1, index2)
    print user
    return

顺便说一句,如果你避免使用你的代码,你的代码会更容易理解和维护lambda除非绝对必要,否则在这种情况下绝对没有必要。使用上面的代码,您可以简化与此绑定:

By the way, your code will be much easier to understand and maintain if you avoid the use of lambda except when absolutely necessary, and it's definitely not necessary in this case. Using the above code, you can simplify the binding to this:

text.tag_bind("click", "<Button-1>", userOptions)

这篇关于Tkinter索引单词问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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