how do i backspace from last number and also clear th? last equation and add a quit button?????????

from tkinter import *
from tkinter.font import Font
def button(frame, text, command=None):
    ft = Font(family=('Verdana'), size=14)
    return Button(frame, text=text, font=ft, width=3, command=command)
def frame(frame, side=LEFT, bg="black"):
    f = Frame(frame, background=bg, padx=5, pady=5)
    f.pack(side=side, expand=YES, fill=BOTH)
    return f
class App:
    def __init__(self, tk):
        ft = Font(family=('Verdana'), size=14)
        main = frame(tk)
        l_frame = frame(main)
        r_frame = frame(main)
        calc_frame = frame(l_frame)
        self.input = Entry(calc_frame, font=ft, width=15, background="white")
        self.input.pack(side=TOP)
        self.btn_frame = frame(calc_frame)
        x, y = 0, 0
        for key in ("()%C", "+-*/", "1234", "5678", "90.="):
            for c in key:
                if c == "=":
                    btn = button(self.btn_frame, c, self.equalAction)
                elif c == "C":
                    btn = button(self.btn_frame, c, self.cleanAction)
                else:
                    btn = button(self.btn_frame, c, lambda i=c: self.input.insert(INSERT, i))
                btn.grid(row=x, column=y)
                y += 1
            x += 1
            y = 0
        self.log = Text(r_frame, font=Font(family=('Verdana'), size=10), width=25, height=14, background="yellow")
        self.log.pack(side=RIGHT)
    def cleanAction(self):
        self.input.delete(0, END)
    def equalAction(self):
        tmp = self.input.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");
if __name__ == '__main__':
    root = Tk()
    root.title("Calculator")
    root.geometry()
    app = App(root)
    root.mainloop()

Recommended Answers

All 6 Replies

You can bind the <BackSpace> key to a function just like any other key. If you want to erase the last character in and entry widget for example, then get the value and set it as value[:-1] For button examples, like a quit button see a tutorial like this one

so how do i do that?

i have tried this

def cleanAction(self):
        self.input.delete(0, END)

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

i got invalid syntax error at else part

You copied two separate answers to your thread from StackOverflowand combined them which yields the indentation mistakes and the orphaned else statement.

so do i need an if statement?

You need to make your own solution, not copy hoping it works, you must try to also understand and adapt the code to your use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.