Gtk spell checking in python with PyGObject on Ubuntu
I've created a little helper application using Python and GTK. I've never used GTK before. As per the comment on http://www.pygtk.org/ I used the PyGObject interface.
Now I would like to add spell checking to my Gtk.TextBuffer.
I found a library called GtkSpell and an associated python-gtkspell in the package manager, but when I try to import it it fails with "ImportError: cannot import name TextView from gtk", I presume this means it is using PyGtk instead of PyGObject.
Is there someway to get this working wit开发者_如何学运维h PyGObject? Or some other premade GTK spellcheck system I can use instead?
I wrote one, yesterday because I had the same problem, so it's a bit alpha but it works fine. You could get the source from: https://github.com/koehlma/pygtkspellcheck. It requires pyenchant and I only test it with Python 3 on Archlinux. If something doesn't work feel free to fill out a bug report on Github.
You have to install it with python3 setup.py install
. It consists of two packages, gtkspellcheck
which does the spellchecking and pylocale
which provides human readable internationalized names for language Codes like de_DE
or en_US
.
Because there is no documentation yet, an example:
# -*- coding:utf-8 -*-
import locale
from gtkspellcheck import SpellChecker, languages, language_exists
from gi.repository import Gtk as gtk
for code, name in languages:
print('code: %5s, language: %s' % (code, name))
window = gtk.Window.new(gtk.WindowType(0))
view = gtk.TextView.new()
if language_exists(locale.getdefaultlocale()[0]):
spellchecker = SpellChecker(view, locale.getdefaultlocale()[0])
else:
spellchecker = SpellChecker(view)
window.set_default_size(600, 400)
window.add(view)
window.show_all()
window.connect('delete-event', lambda widget, event: gtk.main_quit)
gtk.main()
I'm afraid that the PyGObject interface is new enough that GtkSpell hasn't been updated to use it yet. As far as I know there is no other premade GTK spell checker.
精彩评论