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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • skanemupp@yahoo.se

    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(INSER T, END)
    #e.delete(ANCHO R,END)




    from __future__ import division
    import Tkinter
    from Tkinter import *

    mygui = Tkinter.Tk()

    mygui.title("Ca lculator")

    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",comman d=Erase, width=2, height=1)
    b.grid(row=8, column=3, sticky=NSEW)
    b = Button(mygui, text="c",comman d=Backspace, width=2, height=1)
    b.grid(row=8, column=4, sticky=NSEW)
    b = Button(mygui, text="=",comman d=Calc, width=18, height=1)
    b.grid(row=9, column=1, columnspan=4, sticky=NSEW)

    mygui.mainloop( )
  • skanemupp@yahoo.se

    #2
    Re: tkinter, editing an entry, int-value of insert?

    solved this:

    def Backspace():
    st = e.index(INSERT)
    e.delete(st-1,st)

    Comment

    Working...