[Tutor] A Technical Question

Gregor Lingl glingl@aon.at
Thu, 20 Jun 2002 01:21:38 +0200


An interactive session with your app shows:

>>>
app.textArea.get(1.0,10000.0).split('\n')
['Hello World', 'How are you doing?', '. I am doing Very well.', '', '',
'Therehahahaha8', '3', '', '']
>>> # don't know if there is a better way than 10000.0 for length of text
>>> len(app.textArea.get(1.0,10000.0).split('\n'))
9
>>> app.textArea.delete(8.0,9.0)
>>> app.textArea.get(1.0,10000.0).split('\n')
['Hello World', 'How are you doing?', '. I am doing Very well.', '', '',
'Therehahahaha8', '3', '']
>>> len(app.textArea.get(1.0,10000.0).split('\n'))
8
>>> app.textArea.delete(7.0,8.0)
>>> app.textArea.get(1.0,10000.0).split('\n')
['Hello World', 'How are you doing?', '. I am doing Very well.', '', '',
'Therehahahaha8', '']
>>> len(app.textArea.get(1.0,10000.0).split('\n'))
7
>>> app.textArea.delete(6.0,7.0)
>>> # last newline seems undeletable
>>> app.textArea.get(1.0,10000.0).split('\n')
['Hello World', 'How are you doing?', '. I am doing Very well.', '', '', '']
>>>

So I can put this - with minor adaptions - into your delete method:

    def delete(self):
        l = len(self.textArea.get(1.0,10000.0).split('\n'))
        start = str(l-1)+'.0'
        end = str(l)+ '.0'
        print l, start, end  # comment out after testing!
        self.textArea.delete(start,end)

Sure, this is not very elegant, but it works.
In fact even this works:

    def delete(self):
        l = len(self.textArea.get(1.0,10000.0).split('\n'))
        self.textArea.delete(float(l-1),float(l))

... but it certainly reveals one of the less beautiful features of Tk(inter)

Gregor


----- Original Message -----
From: "Chad Crabtree" <flaxeater@yahoo.com>
To: <tutor@python.org>
Sent: Wednesday, June 19, 2002 8:29 PM
Subject: [Tutor] A Technical Question


> I have been playing around with TKinter.  I have done
> pretty much what I have set about.  I made a little
> text box thingy.  Any way I wanted to make a delete
> button that would delete the last line of the text
> box.
> However I can not figure out how to interogate the
> text box so that it tells me how many indexes it has.
>
> Here is my Code right now.  Mind you it's just a toy,
> with pretend levers and switches.
>
> from Tkinter import *
>
> class Application(Frame):
>
>     def right(self):
>         index=str(self.counter) + "\n"
>         self.textArea.insert(END,index)
>         self.counter=self.counter+1
>         self.textArea.see(END)
>
>     def addtotext(self,item):
>         self.textArea.insert(END,item)
>
>
>     def delete(self):
>         self.textArea.insert("DELETE",END)
>
>     def __init__(self, master=None):
>         self.counter=3
>         self.root=Frame.__init__(self,master)
>         self.grid()
>         self.main=Frame(self.root,relief="groove",
> border=5)
>         self.main.grid(row=0,column=0,columnspan=2)
>
>         self.subF=Frame(self.main,
> relief="raised",border=5)
>
> self.subF2=Frame(self.subF,relief="sunken",border=3)
>
>         self.subF.grid(row=0,column=0)
>         self.subF2.grid(row=0,column=0)
>
>
>
self.textArea=Text(self.subF2,height="12",width="30",bg="blue",fg="yellow")
>         self.textArea.grid(column=0,row=0)
>         self.scrBar=Scrollbar(self.subF2)
>         self.scrBar.grid(column=1,row=0,sticky=N+S)
>
> self.scrBar.config(command=self.textArea.yview)
>
> self.textArea.config(yscrollcommand=self.scrBar.set)
>
> self.output=['Hello World\n','How are you doing?\n.
> I am doing Very well.\n\n\nThere
> hahahaha',str(8)+"\n",str(self.counter) + "\n"]
>         self.textArea.insert(END,self.output[0])
>         self.textArea.insert(END,self.output[1])
>         self.textArea.insert(END,self.output[2])
>         self.textArea.insert(END,self.output[3])
>
>
> self.frmButton=Frame(self.root,border=2,relief="sunken")
>
> self.frmButton.grid(row=1,column=0,columnspan=2,sticky=N+E+S+W)
>
>
>         self.quitButton=Button(self.frmButton,text="
> Quit ",command=self.quit)
>
> self.quitButton.grid(column=0,row=0,sticky=N+E+S+W,padx=10)
>
>
>         self.btnWrite=Button(self.frmButton,text="
> Write",command=self.right)
>
> self.btnWrite.grid(column=1,row=0,sticky=N+E+S+W,padx=10)
>
>
> self.btnDel=Button(self.frmButton,text="Delete",command=self.delete)
>
> self.btnDel.grid(column=2,row=0,sticky=N+E+S+W,padx=10)
>
>
>
> app=Application()
> app.master.title("Sample application")
> app.mainloop()
>
> Thank you.
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! - Official partner of 2002 FIFA World Cup
> http://fifaworldcup.yahoo.com
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>