Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
program a calculator
#1
hey guys if you have any suggestion to program a calculator without using class please tell my about
Reply
#2
There are many examples, google: 'basic calculator python'
example: https://www.programiz.com/python-program...calculator
Reply
#3
there is my code:
from tkinter import *
class Calculator():
    def __init__(self):
        self.phase1 = 0
        self.phase2 = 0
        self.final = 0
        self.entry = StringVar()
        self.text = ""
        self.signe = ""
        self.entry.set("made by omar hamidi 2020")

    def init(self):
        self.phase1 = 0
        self.phase2 = 0
        self.final = 0
        self.text = ""
        self.signe = ""
    def afficher_Nb(self):
        self.entry.set(self.text)

    def operation(self):
        try:
            if "+" in self.text:
                self.Plus()
            elif "-" in self.text:
                self.Sous()
            elif "/" in self.text:
                self.Div()
            elif "x" in self.text:
                self.Mult()
            else:
                self.entry.set("                      ERROR")
                self.init()
        except:
            self.entry.set("                      ERROR")
            self.init()

    def Plus(self):
        nb = self.text.split("+")
        self.phase1 = float(nb[0])
        self.phase2 = float(nb[1])
        self.final = self.phase1 + self.phase2
        self.entry.set(str(self.final))
        self.init()

    def Sous(self):
        nb = self.text.split("-")
        self.phase1 = float(nb[0])
        self.phase2 = float(nb[1])
        self.final = self.phase1 - self.phase2
        self.entry.set(str(self.final))
        self.init()

    def Div(self):
        nb = self.text.split("/")
        self.phase1 = float(nb[0])
        self.phase2 = float(nb[1])
        self.final = self.phase1 / self.phase2
        self.entry.set(str(self.final))
        self.init()

    def Mult(self):
        nb = self.text.split("x")
        #self.phase1 = int(nb[0])
        #self.phase2 = int(nb[1])
        #self.final = self.phase1 * self.phase2
        self.final = int(nb[0]) * int(nb[1])
        self.entry.set(str(self.final))
        self.init()


def Button1():
    calculatrice.text += "1"
    calculatrice.entry.set(calculatrice.text)

def Button2():
    calculatrice.text += "2"
    calculatrice.entry.set(calculatrice.text)

def Button3():
    calculatrice.text += "3"
    calculatrice.entry.set(calculatrice.text)

def Button4():
    calculatrice.text += "4"
    calculatrice.entry.set(calculatrice.text)

def Button5():
    calculatrice.text += "5"
    calculatrice.entry.set(calculatrice.text)

def Button6():
    calculatrice.text += "6"
    calculatrice.entry.set(calculatrice.text)

def Button7():
    calculatrice.text += "7"
    calculatrice.entry.set(calculatrice.text)

def Button8():
    calculatrice.text += "8"
    calculatrice.entry.set(calculatrice.text)

def Button9():
    calculatrice.text += "9"
    calculatrice.entry.set(calculatrice.text)

def Button0():
    calculatrice.text += "0"
    calculatrice.entry.set(calculatrice.text)

def ButtonF():
    calculatrice.text += "."
    calculatrice.entry.set(calculatrice.text)

def ButtonP():
    calculatrice.text += "+"
    calculatrice.entry.set(calculatrice.text)

def ButtonS():
    calculatrice.text += "-"
    calculatrice.entry.set(calculatrice.text)

def ButtonD():
    calculatrice.text += "/"
    calculatrice.entry.set(calculatrice.text)

def ButtonM():
    calculatrice.text += "x"
    calculatrice.entry.set(calculatrice.text)

def ButtonE():
    calculatrice.operation()

def ButtonC():
    calculatrice.entry.set("")
    calculatrice.init()


window = Tk()
window.geometry("200x240")
window.title("Calculatrice v1.0")
window["bg"]= "skyblue2"
window["relief"] = "raised"

calculatrice = Calculator()


ECRAN = Entry(window, width=28, textvariable=calculatrice.entry, bg ="black", fg="white", relief=SUNKEN, bd=5).place(x=9, y=8)


B1 = Button(window, text="1", command=Button1, width=3, height=2, bg="grey", fg="white").place(x=10, y=40)
B2 = Button(window, text="2", command=Button2, width=3, height=2, bg="grey", fg="white").place(x=50, y=40)
B3 = Button(window, text="3", command=Button3, width=3, height=2, bg="grey", fg="white").place(x=90, y=40)
B4 = Button(window, text="4", command=Button4, width=3, height=2, bg="grey", fg="white").place(x=10, y=90)
B5 = Button(window, text="5", command=Button5, width=3, height=2, bg="grey", fg="white").place(x=50, y=90)
B6 = Button(window, text="6", command=Button6, width=3, height=2, bg="grey", fg="white").place(x=90, y=90)
B7 = Button(window, text="7", command=Button7, width=3, height=2, bg="grey", fg="white").place(x=10, y=140)
B8 = Button(window, text="8", command=Button8, width=3, height=2, bg="grey", fg="white").place(x=50, y=140)
B9 = Button(window, text="9", command=Button9, width=3, height=2, bg="grey", fg="white").place(x=90, y=140)
BC = Button(window, text="C", command=ButtonC, width=3, height=2, bg="red", fg="white", relief=RIDGE).place(x=10, y=190)
B0 = Button(window, text="0", command=Button0, width=3, height=2, bg="grey", fg="white").place(x=50, y=190)
BF = Button(window, text=".", command=ButtonF, width=3, height=2, bg="grey", fg="white").place(x=90, y=190)

BP = Button(window, text="+", command=ButtonP, width=4, height=2, bg="gold", fg="black", relief=GROOVE).place(x=150, y=40)
BS = Button(window, text="-", command=ButtonS, width=4, height=2, bg="gold", fg="black", relief=GROOVE).place(x=150, y=80)
BD = Button(window, text="/", command=ButtonD, width=4, height=2, bg="gold", fg="black", relief=GROOVE).place(x=150, y=120)
BM = Button(window, text="x", command=ButtonM, width=4, height=2, bg="gold", fg="black", relief=GROOVE).place(x=150, y=160)
BE = Button(window, text="=", command=ButtonE, width=4, height=1, bg="blue", fg="white", relief=RIDGE).place(x=150, y=205)

window.mainloop()
Reply
#4
Without using class.
GUI or non-GUI.
We don't like doing people's homework here but would be happy to help with ideas and help you fix your code. You need to post what you have.
Calculator is a common homework project here, usually non-GUI, so you might start with a menu, then write the code to perform the operations based on the menu choices. When stuck show us what you have
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Program to Make a Simple Calculator jack_sparrow007 2 10,150 Oct-19-2018, 08:32 AM
Last Post: volcano63
  Nead help with my calculator program- getting syntax error FantasyCookie17 2 2,578 Aug-29-2018, 04:25 AM
Last Post: FantasyCookie17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020