[Tutor] Help with tkinter application.

lonetwin lonetwin@yahoo.com
Thu, 17 Jan 2002 17:25:52 +0530


Hi Once Again, :)
   I made a few changes, now the label gets redrawn, also, besides being
   exported to the clipboard, the entry box gets focus after a (correctly
   spelled) word is checked.
   The only problem now is redrawing the listbox or making it go away after
   entering a second word to check.

the code :
==================================================
#!/usr/bin/python
import os
from Tkinter import *

class Spell:
	def __init__(self, word):
		if not word: return
		fin, fout = os.popen2('ispell -a')
		version = fout.readline() or "Ispell version unknown"
		fin.write(word)
		fin.close()
		self.result = self.interpret(fout.read())

	def interpret(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)
		#######################
		#  Change here, made label instance var. instead of local to check()
		# but I don't make it visible now
		self.label = Label(self)

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

		print self.result
		
		
if __name__ == '__main__':
	S = Tkspell()
	S.master.title("Tkspell")
	S.mainloop()

-------------------------------------------------------

Peace
Steve

PS: anyone with me ??....if not plz lemme know, I'll quitely go away :)

-- 
----------------------------------------------
I get up each morning, gather my wits.
Pick up the paper, read the obits.
If I'm not there I know I'm not dead.
So I eat a good breakfast and go back to bed.

Oh, how do I know my youth is all spent?
My get-up-and-go has got-up-and-went.
But in spite of it all, I'm able to grin,
And think of the places my get-up has been.
                -- Pete Seeger
------------------------------------------