[Tutor] Help with tkinter application.

Michael P. Reilly arcege@speakeasy.net
Thu, 17 Jan 2002 08:36:18 -0500


On Thu, Jan 17, 2002 at 05:25:52PM +0530, lonetwin wrote:
> 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.

You can try the 'forget()' method.  But it will mean keeping track of
the listbox as well.  I'd suggest the following changes.

> 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)

                self.resultlist = None

> 
> 	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()

                        # we know there should be nothing in the listbox
                        if self.resultlist:
                            self.resultlist.delete(1, END)
                            self.resultlist.forget()

> 			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))

                        if self.resultlist:
                          ResultList = resultlist # should be empty
                        else:
                          ResultList = Listbox(self, heigh=len(suggestions))
                          self.resultlist = ResultList

> 			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 :)

Some people just need their ten gallons of coffee to answer. :)

  -Arcege