Ahmed Mohsen
Ahmed Mohsen

Reputation: 57

Calculator using Tkinter

I was trying to make a calculator using Tkinter in python 3.8 and I managed to do the Buttons and the label and to make Addition using 2 numbers the problem is I want to make it continue adding if I add more numbers But I couldn't do it. here is a simple version of my code.

code :

import tkinter as tk

first = None
method = None
second  = None
result = None
def OnButtonClick(button_id):
    global first
    global second
    global method
    global result

    if first == None :
        first = button_id
    elif first != None and button_id == "+":
        method = button_id
    elif first != None and second == None and method != None:
        second = button_id

    elif first != None and second != None and button_id == "=":
        result = first + second
        print(result)

window = tk.Tk()
window.title('Calculator')
window.geometry("307x500")

button = window.title('Calculator')
window.geometry("307x500")



B1 = tk.Button(window, text = "1", width = 2, height = 2, command=lambda: OnButtonClick(1) ).grid(row = 7, column = 0)
B2 = tk.Button(window, text = "2", width = 2, height = 2, command=lambda: OnButtonClick(2)).grid(row = 7, column = 1)
B3 = tk.Button(window, text = "3", width = 2, height = 2, command=lambda: OnButtonClick(3)).grid(row = 7, column = 2)
BPlus = tk.Button(window, text = "+", width = 2, height = 2, command=lambda: OnButtonClick("+")).grid(row = 7, column = 3)
BEqual = tk.Button(window, text = "=", width = 2, height = 2, command=lambda: OnButtonClick("=")).grid(row = 8, column = 3)
window.mainloop()

Upvotes: 0

Views: 280

Answers (1)

10 Rep
10 Rep

Reputation: 2270

The way you are doing it is really messy, no offence. In this way, you would need to use a lot of base cases, which might take a long time to code.

A simpler way to do this would be to use the eval(). If the button_id is the equal button, then we can just use eval() to calculate the calculation because we are appending the other button_id's to the string op.

Here is the code:

import tkinter as tk

first = None
method = None
second  = None
result = None
op = ""
def OnButtonClick(button_id):
    global first, second, method, result, op

    if button_id == "=":
        print(eval(op))
    else:
        op += str(button_id)

    

window = tk.Tk()
window.title('Calculator')
window.geometry("307x500")

button = window.title('Calculator')
window.geometry("307x500")



B1 = tk.Button(window, text = "1", width = 2, height = 2, command=lambda: OnButtonClick(1) ).grid(row = 7, column = 0)
B2 = tk.Button(window, text = "2", width = 2, height = 2, command=lambda: OnButtonClick(2)).grid(row = 7, column = 1)
B3 = tk.Button(window, text = "3", width = 2, height = 2, command=lambda: OnButtonClick(3)).grid(row = 7, column = 2)
BPlus = tk.Button(window, text = "+", width = 2, height = 2, command=lambda: OnButtonClick("+")).grid(row = 7, column = 3)
BEqual = tk.Button(window, text = "=", width = 2, height = 2, command=lambda: OnButtonClick("=")).grid(row = 8, column = 3)
window.mainloop()

Hope this helps!

Upvotes: 1

Related Questions