Home>

I have a blank calculator program. I need working buttons so that when they click on them, they enter the desired numbers. You also need the correct answer to be displayed.

class Example (Frame):
def __init __ (self):
    super () .__ init __ ()
    self.initUI ()
def initUI (self):
    self.master.title ("Calculator")
    Style (). Configure ("TButton", padding= (0, 5, 0, 5), font= 'serif 10')
    self.columnconfigure (0, pad= 3)
    self.columnconfigure (1, pad= 3)
    self.columnconfigure (2, pad= 3)
    self.columnconfigure (3, pad= 3)
    self.rowconfigure (0, pad= 3)
    self.rowconfigure (1, pad= 3)
    self.rowconfigure (2, pad= 3)
    self.rowconfigure (3, pad= 3)
    self.rowconfigure (4, pad= 3)
    entry= Entry (self)
    entry.grid (row= 0, columnspan= 4, sticky= W + E)
    cls= Button (self, text= "Clear")
    cls.grid (row= 1, column= 0)
    bck= Button (self, text= "Delete")
    bck.grid (row= 1, column= 1)
    lbl= Button (self)
    lbl.grid (row= 1, column= 2)
    clo= Button (self, text= "Close")
    clo.grid (row= 1, column= 3)
    sev= Button (self, text= "7")
    sev.grid (row= 2, column= 0)
    eig= Button (self, text= "8")
    eig.grid (row= 2, column= 1)
    nin= Button (self, text= "9")
    nin.grid (row= 2, column= 2)
    div= Button (self, text= "/")
    div.grid (row= 2, column= 3)
    fou= Button (self, text= "4")
    fou.grid (row= 3, column= 0)
    fiv= Button (self, text= "5")
    fiv.grid (row= 3, column= 1)
    six= Button (self, text= "6")
    six.grid (row= 3, column= 2)
    mul= Button (self, text= "*")
    mul.grid (row= 3, column= 3)
    one= Button (self, text= "1")
    one.grid (row= 4, column= 0)
    two= Button (self, text= "2")
    two.grid (row= 4, column= 1)
    thr= Button (self, text= "3")
    thr.grid (row= 4, column= 2)
    mns= Button (self, text= "-")
    mns.grid (row= 4, column= 3)
    zer= Button (self, text= "0")
    zer.grid (row= 5, column= 0)
    dot= Button (self, text= ".")
    dot.grid (row= 5, column= 1)
    equ= Button (self, text= "=")
    equ.grid (row= 5, column= 2)
    pls= Button (self, text= "+")
    pls.grid (row= 5, column= 3)
    self.pack ()
  • Answer # 1

    Here is the finished code ( from here):

    from tkinter import *
    class Main (Frame):
        def __init __ (self, root):
            super (Main, self) .__ init __ (root)
            self.build ()
        def build (self):
            self.formula= "0"
            self.lbl= Label (text= self.formula, font= ("Times New Roman", 21, "bold"), bg= "# 000", foreground= "# FFF")
            self.lbl.place (x= 11, y= 50)
            btns= [
                "C", "DEL", "*", "=",
                "1", "2", "3", "/",
                "4", "5", "6", "+",
                "7", "8", "9", "-",
                "(", "0", ")", "X ^ 2"
            ]
            x= 10
            y= 140
            for bt in btns:
                com= lambda x= bt: self.logicalc (x)
                Button (text= bt, bg= "# FFF",
                       font= ("Times New Roman", 15),
                       command= com) .place (x= x, y= y,
                                          width= 115,
                                          height= 79)
                x += 117
                if x >
     400:
                    x= 10
                    y += 81
        def logicalc (self, operation):
            if operation== "C":
                self.formula= ""
            elif operation== "DEL":
                self.formula= self.formula [0: -1]
            elif operation== "X ^ 2":
                self.formula= str ((eval (self.formula)) ** 2)
            elif operation== "=":
                self.formula= str (eval (self.formula))
            else:
                if self.formula== "0":
                    self.formula= ""
                self.formula += operation
            self.update ()
        def update (self):
            if self.formula== "":
                self.formula= "0"
            self.lbl.configure (text= self.formula)
    if __name__== '__main__':
        root= Tk ()
        root ["bg"]= "# 000"
        root.geometry ("485x550 + 200 + 200")
        root.title ("Calculator")
        root.resizable (False, False)
        app= Main (root)
        app.pack ()
        root.mainloop ()
    
  • Answer # 2

    Here is the finished code ( from here):

    from tkinter import *
    class Main (Frame):
        def __init __ (self, root):
            super (Main, self) .__ init __ (root)
            self.build ()
        def build (self):
            self.formula= "0"
            self.lbl= Label (text= self.formula, font= ("Times New Roman", 21, "bold"), bg= "# 000", foreground= "# FFF")
            self.lbl.place (x= 11, y= 50)
            btns= [
                "C", "DEL", "*", "=",
                "1", "2", "3", "/",
                "4", "5", "6", "+",
                "7", "8", "9", "-",
                "(", "0", ")", "X ^ 2"
            ]
            x= 10
            y= 140
            for bt in btns:
                com= lambda x= bt: self.logicalc (x)
                Button (text= bt, bg= "# FFF",
                       font= ("Times New Roman", 15),
                       command= com) .place (x= x, y= y,
                                          width= 115,
                                          height= 79)
                x += 117
                if x >
     400:
                    x= 10
                    y += 81
        def logicalc (self, operation):
            if operation== "C":
                self.formula= ""
            elif operation== "DEL":
                self.formula= self.formula [0: -1]
            elif operation== "X ^ 2":
                self.formula= str ((eval (self.formula)) ** 2)
            elif operation== "=":
                self.formula= str (eval (self.formula))
            else:
                if self.formula== "0":
                    self.formula= ""
                self.formula += operation
            self.update ()
        def update (self):
            if self.formula== "":
                self.formula= "0"
            self.lbl.configure (text= self.formula)
    if __name__== '__main__':
        root= Tk ()
        root ["bg"]= "# 000"
        root.geometry ("485x550 + 200 + 200")
        root.title ("Calculator")
        root.resizable (False, False)
        app= Main (root)
        app.pack ()
        root.mainloop ()