Комментарии:
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()
Thank you
ОтветитьWhile running the code , The columns are showing gap in between eachother
Ответитьit says i need to define "math"
ОтветитьIt says math is not defined for me
Ответитьnice explaination
ОтветитьIs there any way to input decimal digits in this calculator?
ОтветитьGracias profe, excelente trabajo.
Ответитьone thing i noticed is you keep repeating code
like cant we inherit and use functions such as eval() instead the if statement
coooooooooooooooooool
Ответить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.
Ответитьim chain whatching this tutorial. Its amazing. Thank you for your help
ОтветитьThanks a lot!
ОтветитьDo we have any GitHub repo link to see the code ?
ОтветитьWhere can have this source code ??
ОтветитьHow do i add comma's in the input like a extra button ',' and make it work?
Ответитьgreat tutorials...but for this one i have a question..cant we define global variable math once and use it thorughout in various functions?
ОтветитьLike button smashed and subscribed. - What a great tutorial!
Ответитьnice..
Ответить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!
And how can we do when we divide a number by zero?
Ответить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?
ОтветитьThank You So Much!!!
Ответить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
💚💚
Ответитьawesome!
Ответитьstupendous!!
ОтветитьThank you so much!
ОтветитьHow can I include cancel button
Ответитьമച്ചാൻ പൊളി 😍
Ответить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...🙏
just made a calculator thanks sir !!
Ответить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()
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?
Ответить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
Awesome tutorial!! Thank you for being so helpful. I finally got it all hatched out.
ОтветитьThanksss😍♥
ОтветитьBeautifully explained. Learning and enjoying alot! Thanks!
ОтветитьTotally binge watching these right now. Please don't tell everyone how much of a nerd I am...
ОтветитьThanks man
ОтветитьCan we add Circle buttons instead of Square ?
Ответить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...
Ответитьyou are the best teacher in the world
Ответитьsimply AWESOME:)
Ответить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 :)
sir you are awesome your tutorials are sooo good.
Ответить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
Ответить