0

Note: I belive there is an issue with the clear function, I have given up on this code. Please don't spend your time on this.

---

I decided that learning tkinter was a good thing to do, but when trying to make a simple calculator my + button does not do what I think I am telling it to do.

from tkinter import *

font = ('Comic sans', 50) # Haha funny
fNum = 0
global firstNumber
firstNumber=0
secondNumber = 0

root = Tk()

# Window information
root.title("Calculator ☺")

# Display
root.configure(bg='#1a1a1a')
e = Entry(root, width=6, borderwidth=5, font=font)

# Defs and shit
def Click(number):
    current = e.get()
    e.delete(0, END)
    e.insert(0, str(current) + str(number))

def Plus():
    firstNumber = e.get()
    global fNum
    global math
    math = 'addition'
    fNum = int(fNum) + int(firstNumber)
    e.insert(0, fNum)
    e.delete(0, END)    

def Minus():
    firstNumber = e.get()
    global fNum
    global math
    math = 'subtraction'
    fNum = int(fNum) - int(firstNumber)
    e.insert(0, fNum)
    e.delete(0, END)

def Times():
    firstNumber = e.get()
    global fNum
    global math
    math = 'multiplication'
    fNum = int(fNum) * int(firstNumber)
    e.insert(0, fNum)
    e.delete(0, END)

def Over():
    firstNumber = e.get()
    global fNum
    global math
    math = 'division'
    fNum = int(fNum) / int(firstNumber)
    e.insert(0, fNum)
    e.delete(0, END)

def Equal():
    secondNumber = e.get()
    e.delete(0, END)

    if math == 'addition':
        e.insert(0, fNum + int(secondNumber))

    elif math == 'subtraction':
        e.insert(0, fNum - int(secondNumber))

    elif math == 'multiplication':
        e.insert(0, fNum * int(secondNumber))

    elif math == 'division':
        e.insert(0, fNum / int(secondNumber))


def Clear():
    global fNum
    global firstNumber
    global current
    global secondnumber
    firstNumber = 0
    current = 0
    secondnumber = 0
    e.delete(0, END)

# Buttons
button1 = Button(root, text='1', padx=35, pady=35, command=lambda: Click(1), bg='#a1a1a1')
button2 = Button(root, text='2', padx=35, pady=35, command=lambda: Click(2), bg='#a1a1a1')
button3 = Button(root, text='3', padx=35, pady=35, command=lambda: Click(3), bg='#a1a1a1')
button4 = Button(root, text='4', padx=35, pady=35, command=lambda: Click(4), bg='#a1a1a1')
button5 = Button(root, text='5', padx=35, pady=35, command=lambda: Click(5), bg='#a1a1a1')
button6 = Button(root, text='6', padx=35, pady=35, command=lambda: Click(6), bg='#a1a1a1')
button7 = Button(root, text='7', padx=35, pady=35, command=lambda: Click(7), bg='#a1a1a1')
button8 = Button(root, text='8', padx=35, pady=35, command=lambda: Click(8), bg='#a1a1a1')
button9 = Button(root, text='9', padx=35, pady=35, command=lambda: Click(9), bg='#a1a1a1')
button0 = Button(root, text='0', padx=35, pady=35, command=lambda: Click(0), bg='#a1a1a1')

buttonPlus =  Button(root, text='+', padx=34, pady=35, command=Plus, bg='#FF8C00')
buttonMinus = Button(root, text='-', padx=34, pady=35, command=Minus, bg='#FF8C00')
buttonTimes = Button(root, text='*', padx=34, pady=35, command=Times, bg='#FF8C00')
buttonOver = Button(root, text='/', padx=34, pady=35, command=Over, bg='#FF8C00')
buttonEquals = Button(root, text='=', padx=77, pady=35, command=Equal, bg='#FF8C00')
buttonClear = Button(root, text='C', padx=35, pady=35, command=Clear, bg='#a1a1a1')

# Drawing stuff
e.grid(row=0, column=0, columnspan=3, padx=9, pady=5)

button1.grid(row=3, column=0)
button2.grid(row=3, column=1)
button3.grid(row=3, column=2)

button4.grid(row=2, column=0)
button5.grid(row=2, column=1)
button6.grid(row=2, column=2)

button7.grid(row=1, column=0)
button8.grid(row=1, column=1)
button9.grid(row=1, column=2)

button0.grid(row=4, column=0,)
buttonEquals.grid(row=4, column=1, columnspan=2)
buttonClear.grid(row=0, column=3)

buttonPlus.grid(row=1, column=3)
buttonMinus.grid(row=2, column=3)
buttonTimes.grid(row=3, column=3)
buttonOver.grid(row=4, column=3)


root.mainloop()

To clearify, it seems the issue is that the "C" button seems to not work at all and also that when adding numbers it does some math that makes no sense (and that I don't think I'm telling it to do)

Divide05
  • 27
  • 5
  • 1
    In `Clear():` you need to declare all those variables as `global` to be able to modify them. [Python function global variables?](https://stackoverflow.com/a/10588342) – 001 Apr 05 '21 at 16:45
  • 1
    @JohnnyMopp as far as I know using globals is not the best thing to do https://stackoverflow.com/questions/19158339/why-are-global-variables-evil, so that should be avoided – Matiiss Apr 05 '21 at 20:10

0 Answers0