Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

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?

EVERYTHING I SAY MAY NOT BE CORRECT!

I am far from an expert in this so please correct me if I’m wrong.

Quote or tag me so I can see your response

 

 

My Computers -

Main PC -

CPU - i5 10400 | Motherboard - ASUS PRIME B460 PLUS | RAM - Corsair Vengeance LPX 32GB (16GBx2) 3200MHz - Running at 2666Mhz | Graphics Card - Gigabyte GTX 1650 SUPER WINDFORCE OC 4G | Power Supply - CORSAIR CV550 80+ Bronze | Storage- KingSpec 120GB SSD | Seagate Barracuda 500GB 7200RPM HDD | Case - PowerTrain Gaming Case | Color Theme -Black and Red | OS -  Windows 10 Pro 1909

 

Storage PC - (Connected via 1Gbps Network)

CPU - i5 650 | Motherboard - Intel Dreamsys H55 | RAM - Samsung 4GB (2GBx2) - Running at 1066Mhz | Graphics Card - GT 610 | Power Supply - PowerStation 500W | Storage- Seagate Barracuda 500x2GB 7200RPM HDD | Seagate Barracuda 1TB 7200RPM HDD | HITACHI 500GB 7200RPM HDD | WD 500GB 7200RPM HDD | Case - Binch Diac Edition | OS -  Windows 10 Pro 1909

 

PC - (i rarely game on this, so this serves as a local mail server)

CPU - i5 3470 | Motherboard - PEGATRON H61 | RAM - Samsung 8GB (4GBx2) - Running at 1066Mhz | Graphics Card - GT 1030 | Power Supply - PowerStation 500W | Storage- Seagate Barracuda 500x2GB 7200RPM HDD | Case - some case found from a prebuilt ( lol ) | OS -  Windows 7 Ultimate SP1

 

+1 more pc for testing parts - Core 2 Duo E7500 | Samsung 4GB (2GBx2) RAM | GT 210 | HITACHI 60GB HDD | 350W Stock

 

Cameras -

Nikon D5600 + 18-55MM AF-S VR LENS + Tamron 70-300mm F/4-5.6 Di LD Macro 

GoPro Hero 7

 

Camera Gear -

Gimbal - FUNSNAP Capture 3

 

Phones - 

iPod Touch 6th Gen, SAMSUNG GALAXY M11, SAMSUNG GALAXY M01 Core, HUAWEI Y9 2018, HUAWEI Y5 Lite 2018, XIAOMI REDMI GO, HTC Desire 526G+

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 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!

EVERYTHING I SAY MAY NOT BE CORRECT!

I am far from an expert in this so please correct me if I’m wrong.

Quote or tag me so I can see your response

 

 

My Computers -

Main PC -

CPU - i5 10400 | Motherboard - ASUS PRIME B460 PLUS | RAM - Corsair Vengeance LPX 32GB (16GBx2) 3200MHz - Running at 2666Mhz | Graphics Card - Gigabyte GTX 1650 SUPER WINDFORCE OC 4G | Power Supply - CORSAIR CV550 80+ Bronze | Storage- KingSpec 120GB SSD | Seagate Barracuda 500GB 7200RPM HDD | Case - PowerTrain Gaming Case | Color Theme -Black and Red | OS -  Windows 10 Pro 1909

 

Storage PC - (Connected via 1Gbps Network)

CPU - i5 650 | Motherboard - Intel Dreamsys H55 | RAM - Samsung 4GB (2GBx2) - Running at 1066Mhz | Graphics Card - GT 610 | Power Supply - PowerStation 500W | Storage- Seagate Barracuda 500x2GB 7200RPM HDD | Seagate Barracuda 1TB 7200RPM HDD | HITACHI 500GB 7200RPM HDD | WD 500GB 7200RPM HDD | Case - Binch Diac Edition | OS -  Windows 10 Pro 1909

 

PC - (i rarely game on this, so this serves as a local mail server)

CPU - i5 3470 | Motherboard - PEGATRON H61 | RAM - Samsung 8GB (4GBx2) - Running at 1066Mhz | Graphics Card - GT 1030 | Power Supply - PowerStation 500W | Storage- Seagate Barracuda 500x2GB 7200RPM HDD | Case - some case found from a prebuilt ( lol ) | OS -  Windows 7 Ultimate SP1

 

+1 more pc for testing parts - Core 2 Duo E7500 | Samsung 4GB (2GBx2) RAM | GT 210 | HITACHI 60GB HDD | 350W Stock

 

Cameras -

Nikon D5600 + 18-55MM AF-S VR LENS + Tamron 70-300mm F/4-5.6 Di LD Macro 

GoPro Hero 7

 

Camera Gear -

Gimbal - FUNSNAP Capture 3

 

Phones - 

iPod Touch 6th Gen, SAMSUNG GALAXY M11, SAMSUNG GALAXY M01 Core, HUAWEI Y9 2018, HUAWEI Y5 Lite 2018, XIAOMI REDMI GO, HTC Desire 526G+

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 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

√ó