向tkinter文本小部件添加高级功能

向tkinter文本小部件添加高级功能,第1张

向tkinter文本小部件添加高级功能

Tkinter文本小部件非常强大,但是您必须自己做一些高级功能。它没有内置的拼写检查或用于加粗文本等的内置按钮,但是它们非常易于实现。所有功能都在小部件中,您只需要知道如何做即可。

以下示例为您提供了一个按钮,用于切换突出显示的文本的粗体状态-选择一个字符范围,然后单击该按钮以添加然后删除粗体属性。您可以很容易地扩展此示例的字体和颜色。

拼写检查也很容易。下面的示例使用/ usr / share / dict / words中的单词(几乎肯定在Windows
7中不存在,因此您需要提供一个合适的单词列表)。由于它仅进行拼写检查,因此非常简单当您按下空格键时,这仅仅是为了将示例的代码大小保持在最小水平。在现实世界中,您希望在进行拼写检查时更加聪明。

import Tkinter as tkimport tkFontclass App(tk.Tk):    def __init__(self):        tk.Tk.__init__(self)        ## Toolbar        self.toolbar = tk.frame()        self.bold = tk.Button(name="toolbar", text="bold",         borderwidth=1, command=self.OnBold,)        self.bold.pack(in_=self.toolbar, side="left")        ## Main part of the GUI        # I'll use a frame to contain the widget and         # scrollbar; it looks a little nicer that way...        text_frame = tk.frame(borderwidth=1, relief="sunken")        self.text = tk.Text(wrap="word", background="white",       borderwidth=0, highlightthickness=0)        self.vsb = tk.Scrollbar(orient="vertical", borderwidth=1,          command=self.text.yview)        self.text.configure(yscrollcommand=self.vsb.set)        self.vsb.pack(in_=text_frame,side="right", fill="y", expand=False)        self.text.pack(in_=text_frame, side="left", fill="both", expand=True)        self.toolbar.pack(side="top", fill="x")        text_frame.pack(side="bottom", fill="both", expand=True)        # clone the text widget font and use it as a basis for some        # tags        bold_font = tkFont.Font(self.text, self.text.cget("font"))        bold_font.configure(weight="bold")        self.text.tag_configure("bold", font=bold_font)        self.text.tag_configure("misspelled", foreground="red", underline=True)        # set up a binding to do simple spell check. This merely        # checks the previous word when you type a space. For production        # use you'll need to be a bit more intelligent about when        # to do it.        self.text.bind("<space>", self.Spellcheck)        # initialize the spell checking dictionary. YMMV.        self._words=open("/usr/share/dict/words").read().split("n")    def Spellcheck(self, event):        '''Spellcheck the word preceeding the insertion point'''        index = self.text.search(r's', "insert", backwards=True, regexp=True)        if index == "": index ="1.0"        else: index = self.text.index("%s+1c" % index)        word = self.text.get(index, "insert")        if word in self._words: self.text.tag_remove("misspelled", index, "%s+%dc" % (index, len(word)))        else: self.text.tag_add("misspelled", index, "%s+%dc" % (index, len(word)))    def onBold(self):        '''Toggle the bold state of the selected text'''        # toggle the bold state based on the first character        # in the selected range. If bold, unbold it. If not        # bold, bold it.        current_tags = self.text.tag_names("sel.first")        if "bold" in current_tags: # first char is bold, so unbold the range self.text.tag_remove("bold", "sel.first", "sel.last")        else: # first char is normal, so bold the whole selection self.text.tag_add("bold", "sel.first", "sel.last")if __name__ == "__main__":    app=App()    app.mainloop()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5644100.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存