Skip to content Skip to sidebar Skip to footer

How Do I Backspace And Clear The Last Equation And Also A Quit Button?

I tried this: self.btnquit = button(calc_frame, 'Quit', tk.destroy) self.btnquit.pack(side = LEFT) before self.input = ... But it came out invalid syntax. And the backspace only w

Solution 1:

You can bind function to BackSpace in __init__

only when cursor (focus) is in Entry

self.input.bind_all('<BackSpace>', self.cleanInput)

or for all situations

main.bind_all('<BackSpace>', self.cleanInput)

and than you can delete text in Entry and Text

defcleanInput(self, event):
    self.input.delete(0, END)
    self.log.delete(1.0, END)

BTW: the same way you can bind other keys - for example digits

else:
    btn = button(self.btn_frame, c, lambda i=c: self.input.insert(INSERT, i))
    main.bind_all(c, lambda event, i=c:self.input.insert(INSERT, i))

EDIT:

full working code:

(issue: at that moment when cursor is in Entry numbers are inserted twice - normaly and by binding)

# python 2.x#from Tkinter import *#from tkFont import Font# python 3.xfrom tkinter import * 
from tkinter.font import Font

classApp:
    def__init__(self, tk):

        self.tk = tk
        self.tk.title("Calculator")
        #self.tk.geometry()

        self.button_font = Font(family=('Verdana'), size=14)        

        main_frame  = self.create_frame(self.tk)
        left_frame  = self.create_frame(main_frame)
        right_frame = self.create_frame(main_frame)
        calc_frame  = self.create_frame(left_frame)

        self.btnquit = self.create_button(calc_frame, "Quit", self.tk.destroy)
        self.btnquit.pack(side = LEFT)

        self.log = Text(right_frame, font=Font(family=('Verdana'), size=10), width=25, height=14, background="yellow")
        self.log.pack(side=RIGHT)

        self.input_text = StringVar()
        self.input = Entry(calc_frame, font=self.button_font, width=15, background="white", textvariable=self.input_text)
        self.input.pack(side=TOP)

        btn_frame = self.create_frame(calc_frame)

        for x, key inenumerate( ("()%C", "+-*/", "1234", "5678", "90.=") ):
            for y, c inenumerate(key):
                if c == "=":
                    btn = self.create_button(btn_frame, c, self.equalAction)
                elif c == "C":
                    btn = self.create_button(btn_frame, c, self.cleanAction)
                else:
                    btn = self.create_button(btn_frame, c, lambda number=c: self.insertNumber(number))
                    #main.bind_all(c, lambda event, number=c: self.insertNumber(number))
                btn.grid(row=x, column=y)

        self.btn_backspace = self.create_button(btn_frame, "<-", self.deleteLastDigit)
        self.btn_backspace.grid(row=5, column=2, columnspan=2, sticky="we")

        self.btn_loop = self.create_button(btn_frame, "LOOP", self.loopAction)
        self.btn_loop.grid(row=5, column=0, columnspan=2, sticky="we")

        main_frame.bind_all('<BackSpace>', self.cleanAction)
        main_frame.bind_all('<Escape>', self.deleteLastDigit)

    defloopAction(self): 
        bedmas = [ "()", "x^n", "*/", "+-" ]
        for element in bedmas:
            self.log.insert(INSERT,"\n"+element)

    # event=None to use function in command= and in bindingdefdeleteLastDigit(self, event=None): 
        self.input_text.set( self.input_text.get()[:-1] )


    definsertNumber(self, number):
        self.input_text.set( self.input_text.get() + number )

    defcleanAction(self):
        self.input_text.set("")
        self.log.delete(1.0, END)

    defequalAction(self):
        tmp = self.input_text.get()
        try:
            result = tmp + "=" + str(eval(tmp))
            self.log.insert(1.0, result + "\n");
            print(result)
        except Exception:
            self.log.insert(1.0, "Wrong expression\n");

    defcreate_button(self, frame, text, command=None):
        return Button(frame, text=text, font=self.button_font, width=3, command=command)

    defcreate_frame(self, frame, side=LEFT, bg="black"):
        f = Frame(frame, background=bg, padx=5, pady=5)
        f.pack(side=side, expand=YES, fill=BOTH)
        return f

    defrun(self):
        self.tk.mainloop()

#---------------------------------------------------------------------if __name__ == '__main__':
    App(Tk()).run()

Post a Comment for "How Do I Backspace And Clear The Last Equation And Also A Quit Button?"