0

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()
3
Contributors
6
Replies
44
Views
3 Years
Discussion Span
Last Post by pyTony
0

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

Edited by woooee

0

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

0

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

Edited by woooee

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.