wordend并不考虑撇号

时间:2015-01-25 04:11:48

标签: python tkinter

如果我们想分析鼠标光标下的单词,我们可以使用:

text.get('current wordstart', 'current wordend') 

但是,这并没有考虑带撇号的单词 还有其他方法吗?

1 个答案:

答案 0 :(得分:2)

克里斯蒂安·戈尔维泽在comp.lang.tcl结束时为我提供了一个解决方法 这是我的Python 3.4测试代码:

import tkinter
import tkinter.messagebox as messagebox

class Creator(object):

    def __init__(self):

        root = self.root = tkinter.Tk()

        # Main Frame
        f_main = tkinter.Frame(root, borderwidth=6, relief='flat')
        f_main.grid(row=0, column=0, sticky='nsew')

        # Text widget and frame
        f_txt = tkinter.Frame(f_main, borderwidth=2, relief="sunken")
        f_txt.config(width=768, height=768)
        f_txt.pack(padx=4, pady=4, side="bottom", fill="both", expand=True)

        my_txt = self.text = tkinter.Text(f_txt)
        my_txt.config(undo=True, wrap='word')
        my_txt.grid(row=0, column=0, sticky="nsew")
        my_txt.bind("<ButtonRelease-3>", self.test)
        my_txt.focus_set()

    def test(self, event=None):
        txt = str(self.text)
        a = "::tk::TextPrevPos {} current tcl_wordBreakBefore".format(txt)
        b = "::tk::TextNextPos {} current tcl_wordBreakAfter".format(txt)
        ind1 = self.root.tk.eval(a)
        ind2 = self.root.tk.eval(b)
        x = self.text.get(ind1, ind2)
        messagebox.showinfo('info', x)

GUI = Creator()
GUI.root.mainloop()