Il widget di testo Tkinter è straordinariamente potente, ma è necessario eseguire alcune funzioni avanzate. Non ha controllo ortografico incorporato o pulsanti incorporati per il testo in grassetto, ecc, ma sono abbastanza facili da implementare. Tutte le funzionalità sono presenti nel widget, devi solo sapere come farlo.
Nell'esempio seguente viene fornito un pulsante per alternare lo stato grassetto del testo evidenziato: selezionare un intervallo di caratteri, quindi fare clic sul pulsante per aggiungere e quindi rimuovere l'attributo grassetto. Dovrebbe essere piuttosto facile estendere questo esempio per font e colori.
Il controllo ortografico è anche abbastanza semplice. il seguente esempio usa le parole in/usr/share/dict/words (che quasi certamente non esiste su Windows 7, quindi dovrai fornire un elenco di parole adatto) È piuttosto semplicistico in quanto solo spelling-checks quando si preme il tasto dello spazio, ma questo è solo per mantenere le dimensioni del codice dell'esempio a un livello minimo. Nel mondo reale vorrai essere un po 'più intelligente quando eseguirai il controllo ortografico.
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()
La domanda ora non sembra richiedere una risorsa, piuttosto un'implementazione come fanno anche tutte le altre domande su SO. – Trilarion
Sono perplesso dalla chiusura dovuta all'essere fuori tema. Questo non sta chiedendo di aiutare a trovare una risorsa o uno strumento di per sé, si sta chiedendo come può essere implementato qualcosa. Certo, è un po 'ampio, ma la risposta accettata non coinvolge strumenti o librerie esterne, ma descrive semplicemente come utilizzare le funzionalità esistenti. Penso che questa domanda meriti di essere riaperta. –