2010-09-17 26 views
12

我是一個簡單的郵件系統上工作,並且需要將以下添加到Tkinter的文本組件:添加先進的功能,一個Tkinter的文本小

  1. 拼寫檢查
  2. 選項來改變字體(在選定文本)
  3. 選項改變字體顏色(在選擇的文本)
  4. 選項更改字體大小(在選擇的文本)

我unders tand tkinter Text小部件能夠通過標記機制使用多種字體和顏色,但我不明白如何利用這些功能。

如何使用Text小部件的功能實現這些功能?具體而言,我如何更改字體系列,字體的顏色和大小,以及如何使用它來實現類似拼寫檢查的內容,其中拼寫錯誤的單詞的下劃線或顏色與文本的其餘部分不同。

+0

現在的問題似乎並沒有要求一個資源,而是一個實現,因爲所有其他問題也如此。 – Trilarion 2015-02-18 20:06:37

+5

由於被關閉主題,我感到困惑。這並不是要求本身幫助尋找資源或工具,而是要求如何實施。無可否認,這有點寬泛,但接受的答案不涉及外部工具或庫,它僅僅描述如何使用現有的功能。我認爲這個問題值得重新打開。 – 2015-02-18 20:21:48

回答

25

Tkinter文本小部件非常強大,但您必須自己做一些高級功能。它沒有內置的拼寫檢查或內置按鈕來加粗文本等,但它們很容易實現。所有的功能都在小部件中,你只需要知道如何去做。

以下示例爲您提供了一個用於切換突出顯示的文本的粗體狀態的按鈕 - 選擇一系列字符,然後單擊該按鈕添加並刪除粗體屬性。對於字體和顏色的擴展應該很容易。

拼寫檢查也很容易。下面的例子使用/ usr/share/dict/words中的詞語(在Windows 7中幾乎肯定不存在,所以你需要提供一個合適的單詞列表)它很簡單,因爲它只是拼寫檢查當您按下空格鍵時,但這只是爲了將示例的代碼大小保持在最低水平。在現實世界中,當你進行拼寫檢查時,你會希望更聰明一些。

import Tkinter as tk 
import tkFont 

class 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() 
+0

哇!這就是我一直在尋找的!謝謝!沒有真正意識到它會是多麼容易!爲你+1! – 2010-09-17 18:08:05

+0

我不斷收到一個錯誤,說(「sel.first」)是一個糟糕的索引。我該如何解決? – 2010-09-18 05:48:28

+0

對不起,這只是一個錯字,但由於某種原因,我並未將文字設置爲粗體。 – 2010-09-18 05:50:53

4

1)Tk沒有集成的拼寫檢查器。您可能會感興趣PyEnchant

2)3)4)不是那麼困難(請忘記我以前的建議使用wxPython)。您可以傳遞一個tag_config作爲文本小部件插入方法的第3個參數。它定義了這個選擇的配置。

請參閱下面的代碼,它是從Scrolledtext示例和effbot改編的,它們是有關Tk的最佳參考。

""" 
Some text 
hello 
""" 

from Tkinter import * 
from Tkconstants import RIGHT, LEFT, Y, BOTH 
from tkFont import Font 
from ScrolledText import ScrolledText 

def example(): 
    import __main__ 
    from Tkconstants import END 

    stext = ScrolledText(bg='white', height=10) 
    stext.insert(END, __main__.__doc__) 

    f = Font(family="times", size=30, weight="bold") 
    stext.tag_config("font", font=f) 

    stext.insert(END, "Hello", "font") 
    stext.pack(fill=BOTH, side=LEFT, expand=True) 
    stext.focus_set() 
    stext.mainloop() 

if __name__ == "__main__": 
    example() 
+0

好吧,我打算使用wxPython。有什麼想法如何在wx中完成這項工作? – 2010-09-17 05:05:23

+0

忘記我的wxPython建議。感謝effbot,我找到了一個Tk解決方案。我希望它有幫助。最佳 – luc 2010-09-17 08:06:19

+0

除了在插入過程中添加標籤之外,您還可以在運行時使用tag_add添加它們。因此,例如,您可以獲取用戶選擇的字符範圍,並將一個或多個標籤應用於該範圍的文本。 – 2011-01-30 04:59:07