Day 066 - Project 66 : Calculator

ahhhhh, why must you use global?

Is there a way to restart a lesson?
I was working through day 66 but now it just wonā€™t load the repl. Iā€™ve updated my browser (chrome), Iā€™ve closed the browser, reopened it, Iā€™ve tried everything I can think of. Other repls are opening fine.

1 Like

Hey there! Check this out:

4 Likes

thanks! glad to know itā€™s not just me haha

The code isnā€™t working. Nothing is getting printed.

Hey @ApoorvGoyal2!

Can you please provide a link to your Repl so we can look into it further?

2 Likes

I realised that the delay was that the GUI was showing up in the Output tab, not the console!

Solved

Iā€™m attempting to add a decimal point button to the calculator.

Iā€™ve gotten it to the point where it almost works, but it automatically inserts a 0 after that decimal point.

For example : I press 1 . 1 and it gives me 1.01.

There's also this (solution spoilers):


even though it does work (other than inserting the unwanted 0.)

If anyone wouldnā€™t mind taking a look at their leisure hereā€™s a link to a copy.


Nvm I asked ai and it helped me. Sorry.

Dear All, because in some modules before weā€™ve learned the eval()-function and Iā€™ve used this one to do the calcuation:

import tkinter as tk
#Creates the window of the app:
window = tk.Tk()
#Set the title of the window:
window.title("Calculator")
#Window size is a string in pixels width x height
window.geometry("300x300")

# Set global variable
calc_number=""


# Define the label globally
label = tk.Label(window, text=calc_number)
label.grid(row=0, column=1)

# Function to update lagel
def updateLabel(number):
  global calc_number
  calc_number = f"{calc_number}{number}"
  # Update the existing label instead of creating a new one
  label.config(text=calc_number)

# Function to perform calculation
def calc():
  global calc_number
  # Try to safely evaluate the calculation
  try:
      result = eval(calc_number)
  except Exception as e:
      result = "Error"
  label.config(text=f"= {result}")

#Function to clear label
def clear():
  global calc_number
  calc_number = ""
  # Update the existing label instead of creating a new one
  label.config(text=calc_number)

#Start with a empty label
clear()

#Buttons
One = tk.Button( text= "1", command = lambda: updateLabel("1"))
One.grid(row=1, column=0)
Two = tk.Button( text= "2", command = lambda: updateLabel("2"))
Two.grid(row=1, column=1)
Three = tk.Button( text= "3", command = lambda: updateLabel("3")).grid(row=1, column=2)
Four = tk.Button( text= "4", command = lambda: updateLabel("4")).grid(row=2, column=0)
Five = tk.Button( text= "5", command = lambda: updateLabel("5")).grid(row=2, column=1)
Six = tk.Button( text= "6", command = lambda: updateLabel("6")).grid(row=2, column=2)
Seven = tk.Button( text= "7", command = lambda: updateLabel("7")).grid(row=3, column=0)
Eight = tk.Button( text= "8", command = lambda: updateLabel("8")).grid(row=3, column=1)
Nine = tk.Button( text= "9", command = lambda: updateLabel("9")).grid(row=3, column=2)
Zero = tk.Button( text= "0", command = lambda: updateLabel("0")).grid(row=4, column=1)


Plus = tk.Button( text= "+", command = lambda: updateLabel("+")).grid(row=2, column=3)
One = tk.Button( text= "-", command = lambda: updateLabel("-")).grid(row=2, column=4)
Multiply = tk.Button( text= "*", command = lambda: updateLabel("*")).grid(row=3, column=3)
Divide = tk.Button( text= "/", command = lambda: updateLabel("/")).grid(row=3, column=4)
Equal = tk.Button( text= "=", command = calc).grid(row=4, column=3)
Clear = tk.Button( text= "Clear", command = clear).grid(row=4, column=4)
#One.pack()



# Starts the app
tk.mainloop()