-1

Im able to add 2 numbers through the equal function, but i dont know how to change what operator is being used based on what button you clicked.

But it cant add decimal numbers for some reason.

Also there is a couple errors im wondering about.

Error messages from Buttons:

Assigning result of a function call, where the function has no return pylint(assignment-from-no-return)

Image

Error from from tkinter import *:

Unused import enum from wildcard importpylint(unused-wildcard-import)

calculator.py

from tkinter import *

root = Tk()
root.title("Calculator")

#Input box
e = Entry(root, width=70, borderwidth=5)
e.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

def btnclick(number):
    current = e.get()
    e.delete(0, END)
    e.insert(0, str(current) + str(number))

def c():
    e.delete(0, END)

def add():
    firstnum = e.get()
    global fnum
    fnum = int(firstnum)
    e.delete(0, END)

def sub():
    return

def div():
    return

def x():
    return

def eq():
    snum = e.get()
    e.delete(0, END)
    e.insert(0, fnum + int(snum))

def dec():
    current = e.get()
    e.delete(0, END)
    e.insert(0, str(current) + ".")


#Buttons
n1 = Button(root, text="1", padx=40, pady=20, command=lambda: btnclick(1)).grid(row=3, column=0)
n2 = Button(root, text="2", padx=40, pady=20, command=lambda: btnclick(2)).grid(row=3, column=1)
n3 = Button(root, text="3", padx=40, pady=20, command=lambda: btnclick(3)).grid(row=3, column=2)
n4 = Button(root, text="4", padx=40, pady=20, command=lambda: btnclick(4)).grid(row=2, column=0)
n5 = Button(root, text="5", padx=40, pady=20, command=lambda: btnclick(5)).grid(row=2, column=1)
n6 = Button(root, text="6", padx=40, pady=20, command=lambda: btnclick(6)).grid(row=2, column=2)
n7 = Button(root, text="7", padx=40, pady=20, command=lambda: btnclick(7)).grid(row=1, column=0)
n8 = Button(root, text="8", padx=40, pady=20, command=lambda: btnclick(8)).grid(row=1, column=1)
n9 = Button(root, text="9", padx=40, pady=20, command=lambda: btnclick(9)).grid(row=1, column=2)
n0 = Button(root, text="0", padx=40, pady=20, command=lambda: btnclick(0)).grid(row=4, column=0)
add = Button(root, text="+", padx=39, pady=20, command=add).grid(row=4, column=3)
c = Button(root, text="C", padx=40, pady=20, command=c).grid(row=4, column=2)
sub = Button(root, text="-", padx=39, pady=20, command=sub).grid(row=3, column=3)
x = Button(root, text="x", padx=40, pady=20, command=x).grid(row=2, column=3)
div = Button(root, text="/", padx=39, pady=20, command=div).grid(row=1, column=3)
eq = Button(root, text="=", padx=220, pady=20, command=eq).grid(row=5, column=0, columnspan=4)
dec = Button(root, text=".", padx=39, pady=20, command=dec).grid(row=4, column=1)

root.mainloop()
astqx
  • 2,058
  • 1
  • 10
  • 21
rooty
  • 3
  • 4

1 Answers1

1

But it cant add decimal numbers for some reason.

It's because you are doing this:

fnum = int(firstnum)

You are telling it that firstnum is an integer. Try:

fnum = float(firstnum)

but i dont know how to change what operator is being used based on what button you clicked

Many ways to skin this one, but a simple way is to just declare a global variable operator and assign it with a value representative of the operation clicked on.

For example:

def x():
    operator = 'multiplication'
    return

Then in your equal function:

def eq():
    snum = float(e.get())
    e.delete(0, END)
    result = 0
    if operator == "addition":
        result = fnum + snum
    elif operator == "multiplication":
        result = fnum * snum
    # .... etc, etc.... 

    e.insert(0, result)
kamion
  • 461
  • 2
  • 9