Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter索引词问题_Python_Regex_Python 2.7_Indexing_Tkinter - Fatal编程技术网

Python Tkinter索引词问题

Python Tkinter索引词问题,python,regex,python-2.7,indexing,tkinter,Python,Regex,Python 2.7,Indexing,Tkinter,我正在用python 2.7编写一个Tkinter应用程序。我使用wordstart和wordend索引来获取单击的单词。这对常规单词非常有效,但对连字符单词不起作用 这是我的密码: from Tkinter import * master = Tk() def userOptions(event, clickposition): index1 = clickposition + " wordstart" index2 = clickposition + " wordend"

我正在用python 2.7编写一个Tkinter应用程序。我使用wordstart和wordend索引来获取单击的单词。这对常规单词非常有效,但对连字符单词不起作用

这是我的密码:

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()
从Tkinter导入*
master=Tk()
def用户选项(事件,单击位置):
index1=点击位置+“文字开始”
index2=点击位置+“文字结束”
user=text.get(index1,index2)
打印用户
返回
def userEnter(事件):
text.config(cursor=“hand2”)
返回
def用户离开(事件):
text.config(cursor=“arrow”)
返回
文本=文本(主)
text.insert(插入“这是一个句子\n”)
text.insert(插入“这是一个在某些单词之间带有破折号的句子\n”)
text.pack()
text.tag_添加(“单击”、“1.0”、“结束”)
text.tag_bind(“单击“,”,用户输入)
text.tag_bind(“单击“,”,用户离开)
text.tag_bind(“单击“,”,lambda事件:用户选项(事件,text.index(@%d,%d”%(event.x,event.y)))
text.config(状态=已禁用)
master.mainloop()

如何配置此选项,以便打印用户可以打印整个连字符单词,而无需在连字符处拆分?例如,根据单击的位置打印字符串“破折号介于”而不是“破折号”、“介于”或“-”。

您不能修改
“wordstart”
定义“单词”的方式。从官方文件:

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

您可以使用文本小部件的内置搜索功能来查找单词的开头和结尾,无论您如何定义“单词”。您可以搜索正则表达式,因此您可以搜索像
[-\w]
这样的模式来获取破折号或单词字符

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

def用户选项(事件):
count=IntVar()
图案=r'[-\w]+'
#找到“单词”的开头,从后面开始_
#角色点击了
start=“@%d,%d+1c”%(事件x,事件y)
index1=text.search(pattern、start、backwards=True、regexp=True)
#从开头开始,找到结尾并保存
#匹配的字符数。
search(pattern,index1,regexp=True,count=count)
#计算匹配的结束索引
index2=text.index(“%s+%sc”%(index1,count.get())
#获取文本
user=text.get(index1,index2)
打印用户
返回
顺便说一句,如果您避免使用lambda,那么您的代码将更容易理解和维护,除非是在绝对必要的情况下,在这种情况下绝对没有必要使用lambda。使用上述代码,您可以简化对此的绑定:

text.tag\u bind(“单击”,“用户选项”)