Jump to content

Why doesn't my python calculator work?

Go to solution Solved by Eigenvektor,

It usually helps if you provide a bit more info other than "it does not work". You're a developer after all, do some debugging 😉

 

At first glance, your code

    global fnum // HMMMMMM………
    f_num = int(first_number)

 

Their code

    global f_num
    f_num = int(first_number)

 

Hello there! i have been following some python tutorials on youtube.. the youtube channel was guiding us to make a calculator with Tkinter and python.. first, i tried it without watching the video.. i made something similar, but with my code, the 'add' button doesn't work.. i copied some code from the video tutorial and it works fine... what is wrong with my code? and can someone explain me the mistake i have done..

 

Python, Tkinter

 

here is my code:

 

from tkinter import *

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

e = Entry(window, width=35, borderwidth=5)
e.grid(row=0, column=0, columnspan=3, padx=10, pady=10)

def button_add():
    first_number = e.get()
    global fnum
    f_num = int(first_number)
    e.delete(0, END)

def button_click(number):
    e.insert(END, number)
    print(number)

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

def button_equal():
    second_number = e.get()
    e.delete(0, END)
    e.insert(0, f_num + int(second_number))


# all the number buttons
button_1 = Button(window, text="1", command=lambda: button_click(1), padx=40, pady=20)
button_2 = Button(window, text="2", command=lambda: button_click(2), padx=40, pady=20)
button_3 = Button(window, text="3", command=lambda: button_click(3), padx=40, pady=20)
button_4 = Button(window, text="4", command=lambda: button_click(4), padx=40, pady=20)
button_5 = Button(window, text="5", command=lambda: button_click(5), padx=40, pady=20)
button_6 = Button(window, text="6", command=lambda: button_click(6), padx=40, pady=20)
button_7 = Button(window, text="7", command=lambda: button_click(7), padx=40, pady=20)
button_8 = Button(window, text="8", command=lambda: button_click(8), padx=40, pady=20)
button_9 = Button(window, text="9", command=lambda: button_click(9), padx=40, pady=20)
button_0 = Button(window, text="0", command=lambda: button_click(0), padx=40, pady=20)
button_add = Button(window, text="+", command=button_add, padx=39, pady=20)
button_equal = Button(window, text="=", command=button_equal, padx=91, pady=20)
button_clear = Button(window, text="Clear", command=button_clear, padx=79, pady=20)

# row 1
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)

# row 2
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)

# row 3
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)

# row 4
button_0.grid(row=4, column=0)

# other buttons
button_add.grid(row=5, column=0)
button_equal.grid(row=5, column=1, columnspan=2)
button_clear.grid(row=4, column=1, columnspan=2)

window.mainloop()

 

these are the functions used by the creator of that video tutorial: ( as the rest of the code is same )

 

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

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

def button_addd():
    first_number = e.get()
    global f_num
    f_num = int(first_number)
    e.delete(0, END)

def button_equall():
    second_number = e.get()
    e.delete(0, END)
    e.insert(0, f_num + int(second_number))

 

why doesn't my code work? can someone explain?

hey! i know to use a computer

Link to comment
https://linustechtips.com/topic/1334126-why-doesnt-my-python-calculator-work/
Share on other sites

Link to post
Share on other sites

It usually helps if you provide a bit more info other than "it does not work". You're a developer after all, do some debugging 😉

 

At first glance, your code

    global fnum // HMMMMMM………
    f_num = int(first_number)

 

Their code

    global f_num
    f_num = int(first_number)

 

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

Just now, Eigenvektor said:

It usually helps if you provide a bit more info other than "it does not work". You're a developer after all, do some debugging 😉

 

At first glance, your code


    global fnum // HMMMMMM………
    f_num = int(first_number)

 

Their code


    global f_num
    f_num = int(first_number)

 

ohhhhh.... tbh, i feel ashamed of it.. omg! lol! i checked it for over 3 times and i didn't find it! Thank You Very Much!

hey! i know to use a computer

Link to post
Share on other sites

3 minutes ago, hirusha.adikari said:

ohhhhh.... tbh, i feel ashamed of it.. omg! lol! i checked it for over 3 times and i didn't find it! Thank You Very Much!

Eh, it happens, especially if you've read something for a hundred times 😀

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×