467,907 Members | 1,661 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,907 developers. It's quick & easy.

tkinter, editing an entry, int-value of insert?

in this program when using the "c"-button it deletes the last token
entered. i want to delete the token after the mousecursor.

lets say the string is: 12*(81**.5+12) and i put the cursor between
the * and * because i want times .5 not root.
now if i press "c" it deletes ")" which is not what i want. ive tried
coming up with a function that does what i want
but neither of the ways as shown below works.
i want to do something like e.delete(INSERT, INSERT+1) but that doesnt
work because it is string+int and
INSERT+"1" doesnt work either. so how do i get the int-value of
insert?
what is the trick?
def Backspace():
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)


from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title("Calculator")

e = Entry(mygui)
e.grid(row=1, column=1, columnspan=4, sticky=NSEW)

c = Entry(mygui)
c.grid(row=2, column=1, columnspan=4, sticky=NSEW)

def Disp(nstr):
e.insert(INSERT, nstr)

def Calc():
expr=e.get()
c.delete(0, END)
try:
c.insert(END, eval(expr))
except:
c.insert(END, "Not computable")

def Erase():
e.delete(0,END)
c.delete(0, END)

def Backspace():
a=len(e.get())
e.delete(a-1,END)

x = 1
y = 4
for char in '123+456-789*0()/.':
b = Button(mygui, text=char, command=lambda n=char:Disp(n),
width=2, height=1)
b.grid(row=y, column=x, sticky=NSEW)
x=x+1
if x==5:
x=1
y=y+1

b = Button(mygui, text="^", command=lambda n="**":Disp(n), width=2,
height=1)
b.grid(row=8, column=2, sticky=NSEW)
b = Button(mygui, text="C",command=Erase, width=2, height=1)
b.grid(row=8, column=3, sticky=NSEW)
b = Button(mygui, text="c",command=Backspace, width=2, height=1)
b.grid(row=8, column=4, sticky=NSEW)
b = Button(mygui, text="=",command=Calc, width=18, height=1)
b.grid(row=9, column=1, columnspan=4, sticky=NSEW)

mygui.mainloop()
Jun 27 '08 #1
  • viewed: 2820
Share:
1 Reply
solved this:

def Backspace():
st = e.index(INSERT)
e.delete(st-1,st)
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Thomas NŁcker | last post: by
7 posts views Thread by William Gill | last post: by
3 posts views Thread by Matt Hammond | last post: by
3 posts views Thread by William Gill | last post: by
2 posts views Thread by Martyn Quick | last post: by
1 post views Thread by Michael Yanowitz | last post: by
3 posts views Thread by dwelch91 | last post: by
reply views Thread by Leonhard Vogt | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.