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

skanemupp at yahoo.se skanemupp at yahoo.se
Sat Apr 12 07:13:08 EDT 2008


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



More information about the Python-list mailing list