Home > database >  This Python Tkinter calculator I made isn't working properly
This Python Tkinter calculator I made isn't working properly

Time:04-15

This Tkinter Calculator I made isn't working properly.

This is the code for it:

from tkinter import *

root = Tk()
root.geometry("350x200")
root.title("Calculator")

e1 = Entry(root, width=35, borderwidth=5)
e1.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
e1.insert(0, "0")

e2 = Entry(root, width=35, borderwidth=5)
e2.grid(row=1, column=0, columnspan=3, padx=10, pady=10)
e2.insert(0, "0")

First_number = float(e1.get())
Second_number = float(e2.get())

add = Button(root, text="Add", padx=10, pady=10, command=lambda: add(First_number, Second_number))
add.grid(row=3, column=0)

sub = Button(root, text="Subtract", padx=10, pady=10, command=lambda: sub(First_number, Second_number))
sub.grid(row=3, column=1)

mul = Button(root, text="Multiply", padx=10, pady=10, command=lambda: mul(First_number, Second_number))
mul.grid(row=3, column=2)

div = Button(root, text="Divide", padx=10, pady=10, command=lambda: div(First_number, Second_number))
div.grid(row=3, column=3)

e3 = Entry(root, width=35, borderwidth=5, font=("Arial", 10))
e3.grid(row=4, column=0, columnspan=3, padx=10, pady=20)
e3.insert(0, "0")

def add(First_number, Second_number):
    e3.delete(0, END)
    e3.insert(0, First_number   Second_number)

def sub(First_number, Second_number):
    e3.delete(0, END)
    e3.insert(0, First_number - Second_number)

def mul(First_number, Second_number):
    e3.delete(0, END)
    e3.insert(0, First_number * Second_number)

def div(First_number, Second_number):
    e3.delete(0, END)
    e3.insert(0, First_number / Second_number)            

root.mainloop()

When the program is run, The widgets show up correctly and the input boxes work, but After I input the first and second numbers and click on an operation (add, subtract, multiply, or divide) the input box for the answer shows 0.0

What should I do?

CodePudding user response:

This is because you set values of First_number and Second_number before entering the value in entry the default value of entry is 0 which is set as the First_number and Second_number values. Then you get the 0 in all the situations and ZeroDivisionError in division.

One more suggestion is your function and button names are the same. Which is not a good practice. You can change the name add to add_btn and sub to sub_btn etc.

This is the formatted code.

from tkinter import *


def add():

    First_number = float(e1.get())
    Second_number = float(e2.get())
    e3.delete(0, END)
    e3.insert(0, First_number   Second_number)

def sub():

    First_number = float(e1.get())
    Second_number = float(e2.get())
    e3.delete(0, END)
    e3.insert(0, First_number - Second_number)

def mul():

    First_number = float(e1.get())
    Second_number = float(e2.get())
    e3.delete(0, END)
    e3.insert(0, First_number * Second_number)

def div():

    First_number = float(e1.get())
    Second_number = float(e2.get())
    e3.delete(0, END)
    e3.insert(0, First_number / Second_number)            

root = Tk()
root.geometry("350x200")
root.title("Calculator")

e1 = Entry(root, width=35, borderwidth=5)
e1.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
e1.insert(0, "0")

e2 = Entry(root, width=35, borderwidth=5)
e2.grid(row=1, column=0, columnspan=3, padx=10, pady=10)
e2.insert(0, "0")


add_btn = Button(root, text="Add", padx=10, pady=10, command=add)
add_btn.grid(row=3, column=0)

sub_btn = Button(root, text="Subtract", padx=10, pady=10, command=sub)
sub_btn.grid(row=3, column=1)

mul_btn = Button(root, text="Multiply", padx=10, pady=10, command=mul)
mul_btn.grid(row=3, column=2)

div_btn = Button(root, text="Divide", padx=10, pady=10, command=div)
div_btn.grid(row=3, column=3)

e3 = Entry(root, width=35, borderwidth=5, font=("Arial", 10))
e3.grid(row=4, column=0, columnspan=3, padx=10, pady=20)
e3.insert(0, "0")

root.mainloop()

One more way is using global. I suggest you use the above case because using the global keyword is not a good practice.

from tkinter import *
First_number = 0
Second_number = 0
def first_second():
    global First_number,Second_number
    First_number = float(e1.get())
    Second_number = float(e2.get())
def add():
    e3.delete(0, END)
    e3.insert(0, First_number   Second_number)

def sub():
    e3.delete(0, END)
    e3.insert(0, First_number - Second_number)

def mul():
    e3.delete(0, END)
    e3.insert(0, First_number * Second_number)

def div():
    e3.delete(0, END)
    e3.insert(0, First_number / Second_number)            

root = Tk()
root.geometry("350x200")
root.title("Calculator")

e1 = Entry(root, width=35, borderwidth=5)
e1.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
e1.insert(0, "0")

e2 = Entry(root, width=35, borderwidth=5)
e2.grid(row=1, column=0, columnspan=3, padx=10, pady=10)
e2.insert(0, "0")


add_btn = Button(root, text="Add", padx=10, pady=10, command=lambda :[first_second(),add()])
add_btn.grid(row=3, column=0)

sub_btn = Button(root, text="Subtract", padx=10, pady=10, command=lambda :[first_second(),sub()])
sub_btn.grid(row=3, column=1)

mul_btn = Button(root, text="Multiply", padx=10, pady=10, command=lambda :[first_second(),mul()])
mul_btn.grid(row=3, column=2)

div_btn = Button(root, text="Divide", padx=10, pady=10, command=lambda :[first_second(),div()])
div_btn.grid(row=3, column=3)

e3 = Entry(root, width=35, borderwidth=5, font=("Arial", 10))
e3.grid(row=4, column=0, columnspan=3, padx=10, pady=20)
e3.insert(0, "0")


  • Related