[Tutor] Help with tkinter application.

lonetwin lonetwin <lonetwin@yahoo.com>
Fri, 18 Jan 2002 13:54:01 +0530 (IST)


Hey Arcege,
   Thanx for the reply, although it didn't quite work as you suggested, I got
the general idea. Thanx to you TKSpell is ready. Have a look below ....you
may even want to run it :) !!!
   Also Eve, thanx for trying it out, the error you mentioned:

===============================================
 python tkinter_spell.py
  File "tkinter_spell.py", line 51
    label = Label(self, text=' '.join([ x for x in self.result]))
                                            ^
SyntaxError: invalid syntax
===============================================
    most probably is because you are running python version 1.5, which does
not understand list comprehensions which are just a short cut way of doing a
particular thing to elements of a list.

Eg:
>>> l = [ "A", "B", "C" ]
>>> p = [ x.lower() for x in l ]
>>> p
['a', 'b', 'c']

       makes sense ?? if it doesn't, ask.

Anyways, here's TKSpell, for anybody who care :)

 Peace
 Steve

========================================================
#!/usr/bin/python
# Written by lonetwin<lonetwin@yahoo.com>
import os
from Tkinter import *

Ispell = "/usr/bin/ispell"

class Spell:
    global Ispell
    def __init__(self, word):
        if not word: return
        fin, fout = os.popen2('%s -a' % Ispell)
        version = fout.readline()
        if version == '':
            print "ispell not found at %s" % Ispell
            os.sys.exit(1)
        fin.write(word)
        fin.close()
        self.result = self.intepret(fout.read())

    def intepret(self, output):
        if output[0] == '*':
            return ('OK',)
        if output[0] == '+':
            return ('Root', output[2:].strip())
        if output[0] == '-':
            return ('Compound', output[2:].strip())
        if output[0] == '&':
            return ('Miss', output[2:].strip())
        if output[0] == '?':
            return ('Guess', output[2:].strip())
        if output[0] == '#':
            return ('Not Found',)

    def getResult(self):
        return self.result

class Tkspell(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid(ipadx=4, ipady=4)
        self.createWidgets()

    def createWidgets(self):
        self.entry = Entry(self)
        self.entry.grid(column=0, row=0, padx=8)
        self.entry.focus()

        self.Ok = Button(self, text="Check", activeforeground="blue",
                         command=self.check)
        self.Ok.bind("<Return>", self.check)
        self.Ok.grid(column=1, row=0)
        self.label = Label(self)
        self.ResultList = Listbox(self, selectmode=SINGLE)

    def check(self, event):
        self.result = Spell(self.entry.get()).getResult()
        self.label['text']= self.result[0]
        self.ResultList.delete(0, END)
        if self.result[0] in ['OK', 'Root', 'Compound', 'Not Found']:
            self.label.grid(column=0, row=1, columnspan=2)
            self.entry.selection_range(0, END)
            self.entry.focus()
            self.ResultList.grid_forget()
            return 0
        elif self.result[0] in ['Miss', 'Guess']:
            suggestions = self.result[1].split(":")[1].split(',')
            self.label.grid(column=0, row=1, columnspan=2)
            self.ResultList['height']=len(suggestions)
            for x in suggestions:
                self.ResultList.insert(END, x)
            self.ResultList.selection_set(0)
            self.ResultList.grid(column=0, row=2, columnspan=2,
                                 pady=4, sticky=EW)
            self.entry.focus()
        # print self.result


if __name__ == '__main__':
    S = Tkspell()
    S.master.title("Tkspell")
    S.mainloop()
==========================================================================
-- 
Nusbaum's Rule:
	The more pretentious the corporate name, the smaller the
	organization.  (For instance, the Murphy Center for the
	Codification of Human and Organizational Law, contrasted
	to IBM, GM, and AT&T.)
----------------------------------------------