Finish Building A Simple Calculator App - Python Tkinter GUI Tutorial #7

Finish Building A Simple Calculator App - Python Tkinter GUI Tutorial #7

Codemy.com

5 лет назад

108,764 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@abdallahabusamaha7846
@abdallahabusamaha7846 - 30.11.2023 06:34

from tkinter import *

root = Tk()
root.title("Simple Calculator")

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

def number_button_command(e, text):
current = e.get()
if text == 'c':
e.delete(0, END)
elif text == '=':
try:
result = eval(current)
e.delete(0, END)
e.insert(0, str(result))
except Exception as e:
e.delete(0, END)
e.insert(0, "Error")
else:
e.insert(END, text)

row = 1
col = 0

Button_Texts = ['7', '8', '9', '/', '4', '5', '6', '*', '3', '2', '1', '-', 'c', '0', '=', '+']

for text in Button_Texts:
btn = Button(root, text=f"{text}", padx=40, pady=20, command=lambda text=text: number_button_command(e, text))
btn.grid(row=row, column=col, sticky='nsew', padx=1, pady=1)
if col == 3:
row += 1
col = 0
else:
col += 1

root.mainloop()

Ответить
@tor6953
@tor6953 - 24.11.2023 17:01

Thank you

Ответить
@kpoornodaysriram5568
@kpoornodaysriram5568 - 12.11.2023 11:59

While running the code , The columns are showing gap in between eachother

Ответить
@lukaatanasoski9257
@lukaatanasoski9257 - 08.10.2023 09:52

it says i need to define "math"

Ответить
@HazptMedia
@HazptMedia - 26.07.2023 10:17

It says math is not defined for me

Ответить
@user-um1vx5bo5z
@user-um1vx5bo5z - 23.07.2023 19:19

nice explaination

Ответить
@rehanrimsha1774
@rehanrimsha1774 - 10.07.2023 13:38

Is there any way to input decimal digits in this calculator?

Ответить
@jorgebalagueraespinosa5936
@jorgebalagueraespinosa5936 - 10.04.2023 14:30

Gracias profe, excelente trabajo.

Ответить
@tinotendagarwe4929
@tinotendagarwe4929 - 01.02.2023 13:00

one thing i noticed is you keep repeating code
like cant we inherit and use functions such as eval() instead the if statement

Ответить
@raymondzhao9557
@raymondzhao9557 - 02.01.2023 16:54

coooooooooooooooooool

Ответить
@RufusVidS
@RufusVidS - 02.10.2022 20:38

Codemy prices have gone up substantially since this recording. The $27 mentioned (with discount) only covers ONE course ($29 actually), not ALL the courses as stated in the video. I am not saying the new price is not worth it, but it's an investment for me I don't want to pay that for just a single course either.

Ответить
@janpaweii3115
@janpaweii3115 - 16.09.2022 06:57

im chain whatching this tutorial. Its amazing. Thank you for your help

Ответить
@user-qc6xl2kf2z
@user-qc6xl2kf2z - 13.09.2022 16:50

Thanks a lot!

Ответить
@rajeshyadlapalli5238
@rajeshyadlapalli5238 - 27.07.2022 13:19

Do we have any GitHub repo link to see the code ?

Ответить
@VickySingh-ig1kl
@VickySingh-ig1kl - 16.07.2022 14:06

Where can have this source code ??

Ответить
@larsdierickx7543
@larsdierickx7543 - 24.05.2022 15:25

How do i add comma's in the input like a extra button ',' and make it work?

Ответить
@ayushshaw3153
@ayushshaw3153 - 05.05.2022 21:17

great tutorials...but for this one i have a question..cant we define global variable math once and use it thorughout in various functions?

Ответить
@RonaldModesitt
@RonaldModesitt - 14.03.2022 04:09

Like button smashed and subscribed. - What a great tutorial!

Ответить
@matthewkeen6281
@matthewkeen6281 - 02.03.2022 18:22

nice..

Ответить
@mardan9111994
@mardan9111994 - 18.02.2022 11:18

Now that's what I call a quality work! Thank you so much, John, I am very new to your channel, but I am enjoying it so much! Already learnt a lot of stuff about GUI through your videos, and I have just started! Keep up the good job, sir!

P.S. I was also wondering if there is a way to store a previous operation in a memory by means of consecutive "+" button clicking or smth similar (without clicking "=")? Say for example, I type 2, then +, then 3, then again +, then 4: so that I could get 9 instead of 7.
I am not that good with algorythms, however I am learning. Thank you in advance, John!

Ответить
@DavSetYT
@DavSetYT - 14.02.2022 16:49

And how can we do when we divide a number by zero?

Ответить
@bob_bobsen
@bob_bobsen - 11.01.2022 16:39

Hey man. Been trying to create an Ans button like on Casio scientific calculators where any time you press '=', you store the value in an Answer variable that can be recalled by the button. How would I port it over to another function?

Ответить
@kwizerafrank4978
@kwizerafrank4978 - 24.12.2021 20:41

Thank You So Much!!!

Ответить
@ansh4786
@ansh4786 - 14.12.2021 18:54

I finally made it but i want to know how to color buttons when we click on them and when they are not clicked also how to color the text please make a video if you haven't or please share the link of video if you've made because its very difficult to find a video from a group of 202 videos.

Thank you

Ответить
@user-ds9ey8pp5h
@user-ds9ey8pp5h - 06.11.2021 15:43

💚💚

Ответить
@wl7573
@wl7573 - 26.10.2021 03:07

awesome!

Ответить
@deepakchowdary1253
@deepakchowdary1253 - 16.08.2021 17:31

stupendous!!

Ответить
@ihsankamil420
@ihsankamil420 - 29.06.2021 14:56

Thank you so much!

Ответить
@harishraghavans3381
@harishraghavans3381 - 09.06.2021 16:14

How can I include cancel button

Ответить
@harikrishnan5756
@harikrishnan5756 - 06.06.2021 18:33

മച്ചാൻ പൊളി 😍

Ответить
@daggercentral9818
@daggercentral9818 - 06.05.2021 07:12

def b_add():
fst_num = e.get()
global f_num
f_num = int(fst_num)
e.delete(0, END)
def b_equal():
scd_num = e.get()
e.delete(0, END)
e.insert(0, f_num + int(scd_num))

in here the code shows f_num is not defined in the line: e.insert(0, f_num + int(scd_num))
please help sir...🙏

Ответить
@RandomGigabytes48
@RandomGigabytes48 - 17.03.2021 20:58

just made a calculator thanks sir !!

Ответить
@cyberpunk3666
@cyberpunk3666 - 01.03.2021 12:52

My version of calculator:

from tkinter import *

main = Tk()
main.title("Calculator")
textbox = Entry(main, font=("Arial", 20), width=20)
textbox.grid(row=0, column=0, columnspan=5)
space = Label(main, text=" ").grid(row=1, column=1, columnspan=5)


def clearing():
textbox.delete(0, END)

def click(number):
textbox.insert(END, str(number))


def adding():
first_number = textbox.get()
global f_num,status
status="+"
f_num=float(first_number)
textbox.delete(0,END)

def multiplying():
first_number = textbox.get()
global f_num,status
status = "*"
f_num=float(first_number)
textbox.delete(0,END)

def subtracting():
first_number = textbox.get()
global f_num,status
status = "-"
f_num=float(first_number)
textbox.delete(0,END)

def dividing():
first_number = textbox.get()
global f_num,status
status = "/"
f_num=float(first_number)
textbox.delete(0,END)

def equal():
second_number=textbox.get()
textbox.delete(0, END)
if status=="+":
textbox.insert(0,float(f_num)+float(second_number))

elif status == "-":
textbox.insert(0, float(f_num) - float(second_number))

elif status == "*":
textbox.insert(0, float(f_num) * float(second_number))

elif status == "/":
textbox.insert(0, float(f_num) / float(second_number))

button_7 = Button(main, text=7, padx=20, pady=20, bg="White", command=lambda: click(7)).grid(row=2, column=0)
button_8 = Button(main, text=8, padx=20, pady=20, bg="White", command=lambda: click(8)).grid(row=2, column=1)
button_9 = Button(main, text=9, padx=20, pady=20, bg="White", command=lambda: click(9)).grid(row=2, column=2)
button_4 = Button(main, text=4, padx=20, pady=20, bg="White", command=lambda: click(4)).grid(row=3, column=0)
button_5 = Button(main, text=5, padx=20, pady=20, bg="White", command=lambda: click(5)).grid(row=3, column=1)
button_6 = Button(main, text=6, padx=20, pady=20, bg="White", command=lambda: click(6)).grid(row=3, column=2)
button_1 = Button(main, text=1, padx=20, pady=20, bg="White", command=lambda: click(1)).grid(row=4, column=0)
button_2 = Button(main, text=2, padx=20, pady=20, bg="White", command=lambda: click(2)).grid(row=4, column=1)
button_3 = Button(main, text=3, padx=20, pady=20, bg="White", command=lambda: click(3)).grid(row=4, column=2)
button_0 = Button(main, text=0, padx=20, pady=20, bg="White", command=lambda: click(0)).grid(row=5, column=1)
button_dot = Button(main, text=".", padx=20, pady=20, bg="White", command=lambda: click(".")).grid(row=5, column=2)
button_clear = Button(main, text="Clear", padx=40, pady=20, bg="White", command=clearing).grid(row=2, column=3,columnspan=2)
button_plus = Button(main, text="+", padx=20, pady=40, bg="White", command=adding).grid(row=3, column=4,rowspan=2)
button_minus = Button(main, text="-", padx=20, pady=20, bg="White", command=subtracting).grid(row=3, column=3)
button_multiply = Button(main, text="*", padx=20, pady=20, bg="White", command=multiplying).grid(row=4, column=3)
button_divide = Button(main, text="/", padx=20, pady=20, bg="White", command=dividing).grid(row=5, column=3)
button_equalto = Button(main, text="=", padx=20, pady=20, bg="White", command=equal).grid(row=5, column=4)

main.mainloop()

Ответить
@alemiggaming4140
@alemiggaming4140 - 24.01.2021 23:21

How can I make the calculator in a way where after you click equals, the first thing you type in AFTER that removes the answer to the previous problem, instead of always having to click clear afterwards?

Ответить
@khushidhingra6524
@khushidhingra6524 - 23.01.2021 07:12

Thank you for providing such videos for 'free'.
Loved how you taught us to build the fully functional calculator!
But there is are some problems that you might face while using the calculator.
You can't perform calculations on floats.
And if you divide something by zero it will give you an error.
This is how you can solve both of these problems-

1. Just change int into float.
2. You can put the divide function into a try block and perform exception handing on it.

Hope this helped you

Ответить
@meltonenterprises6761
@meltonenterprises6761 - 20.01.2021 23:27

Awesome tutorial!! Thank you for being so helpful. I finally got it all hatched out.

Ответить
@wraksyt9126
@wraksyt9126 - 16.01.2021 17:56

Thanksss😍♥

Ответить
@alihabib3201
@alihabib3201 - 11.01.2021 02:57

Beautifully explained. Learning and enjoying alot! Thanks!

Ответить
@discsanddata6021
@discsanddata6021 - 29.12.2020 04:37

Totally binge watching these right now. Please don't tell everyone how much of a nerd I am...

Ответить
@talhazulfiqar571
@talhazulfiqar571 - 25.12.2020 02:57

Thanks man

Ответить
@aryanvaghasiya2342
@aryanvaghasiya2342 - 15.12.2020 13:41

Can we add Circle buttons instead of Square ?

Ответить
@pimmpoll1471
@pimmpoll1471 - 06.12.2020 20:34

Getting confused, how did the calculator know that it needs to multiply or divide? The global variable math is only been given a string which "substract" etc...

Ответить
@sanjaysanjay862
@sanjaysanjay862 - 25.11.2020 18:13

you are the best teacher in the world

Ответить
@sanjaysanjay862
@sanjaysanjay862 - 25.11.2020 18:10

simply AWESOME:)

Ответить
@siddhantojha2452
@siddhantojha2452 - 24.11.2020 19:01

Thank you for your video... I literally have a school project I need to submit tomorrow and your video has helped a lot. Thanks💚

I also smashed the like button :)

Ответить
@vamprock02
@vamprock02 - 07.11.2020 20:22

sir you are awesome your tutorials are sooo good.

Ответить
@abdullahmohsen8866
@abdullahmohsen8866 - 05.11.2020 07:00

thanks man, that was great but there is only one problem, when I for example add 2 + 3 it equals 5 and that's nice, but when the process is done and i kept pressing the equal button it will keep adding numbers by itself. will be grateful if you told me why is that and how to fix it

Ответить