Python Forum
Scientific Calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scientific Calculator
#1
Hi,

Im new with coding and python and try to rebuild a scientific calculator.
I have the buttons and screen etc, but now i want to add commands to the buttons.
When i run the code and press one of the number buttons it wont show up in the window.
Every hint is welcome!


from tkinter import *
import math
import parser
import tkinter.messagebox


mainFr = Tk()
mainFr.title("CalcPy v1.1")
operator = ""
mainFr.configure(background="gray8")
mainFr.resizable(width=FALSE, height=FALSE)
mainFr.geometry("440x500+0+0")


calc = Frame(mainFr)
calc.grid()

class Rekenmachine():
    def __init__(self):
        self.total = 0
        self.current = ""
        self.input_value = True
        self.check_sum = False
        self.op = ""
        self.results = False

    def numberEnter(self, num):
        self.reuslt = False
        firstnum = txtDisplay.get()
        secondnum = str(num)
        if self.input_value:
            self.current = secondnum
            self.input_value = False
        else:
            if secondnum == '.':
                if secondnum in firstnum:
                    return
            self.current = firstnum + secondnum
        self.display(self.current)


    def display(self, value):
        txtDisplay.delete(0, END)
        txtDisplay.insert(0, value)


added_value = calc


#===============DISPLAY==================================
txtDisplay = Entry(calc, font=("arial", 20, "bold"), bd=10, width=28, justify=RIGHT)
txtDisplay.grid(row=0, column=0, columnspan=4, pady=1)
txtDisplay.insert(0, "0")






#===============BUTTON BASIC===================================
numberpad = "789456123"
i = 0
btn = []
for j in range(2, 5):
    for k in range(3):
        btn.append(Button(calc, width=6, height=2, font=("arial", 20, "bold"), bg="gray75", text=numberpad[i]))
        btn[i].grid(row=j, column=k, pady=1)




#================LAST ADDED CODE ======================================================
        btn[i] ["command"] = lambda x= numberpad[i]: added_value.numberEnter(x)
#=======================================================================================



        i += 1




btnClear = Button(calc, text=chr(67), width=6, height=2,font=("arial", 20, "bold"), bg="OrangeRed2").grid(row=1, column=0, pady=1)
btnClall = Button(calc, text=chr(67) + chr(69), width=6, height=2,font=("arial", 20, "bold"), bg="OrangeRed3").grid(row=1, column=1, pady=1)
btnRoot = Button(calc, text="√", width=6, height=2,font=("arial", 20, "bold"), bg="orange").grid(row=1, column=2, pady=1)
btnAdd = Button(calc, text="+", width=6, height=2,font=("arial", 20, "bold"), bg="orange").grid(row=1, column=3, pady=1)
btnMin = Button(calc, text="-", width=6, height=2,font=("arial", 20, "bold"), bg="orange").grid(row=2, column=3, pady=1)
btnDef = Button(calc, text="/", width=6, height=2,font=("arial", 20, "bold"), bg="orange").grid(row=3, column=3, pady=1)
btnX = Button(calc, text="x", width=6, height=2,font=("arial", 20, "bold"), bg="orange").grid(row=4, column=3, pady=1)
btnZero = Button(calc, text=",", width=6, height=2,font=("arial", 20, "bold"), bg="orange").grid(row=5, column=0, pady=1)
btnDot = Button(calc, text="0", width=6, height=2,font=("arial", 20, "bold"), bg="gray75").grid(row=5, column=1, pady=1)
btnSpec = Button(calc, text=chr(177), width=6, height=2,font=("arial", 20, "bold"), bg="orange").grid(row=5, column=2, pady=1)
btnEq = Button(calc, text="=", width=6, height=2,font=("arial", 20, "bold"), bg="orange").grid(row=5, column=3, pady=1)
Reply
#2
You have made
class Rekenmachine()
but you haven't instantiated it anywhere. To use the functions of that class you must make an instance of that class.
Reply
#3
@Clunk_Head can u give me the example so I can understand it better. Do I need to call the class: Rekenmachine?
Reply
#4
(Dec-04-2019, 08:44 PM)Spacecowboy Wrote: @Clunk_Head can u give me the example so I can understand it better. Do I need to call the class: Rekenmachine?

Happy to, but can you post the directions to the assignment first? You may not need to use the class.
Reply
#5
##UPDATE##

@ line 47 changed to : added_value = Rekenmachine

There is now an error
   
btn[i] ["command"] = lambda x= numberpad[i]: added_value.numberEnter(x)
TypeError: numberEnter() missing 1 required positional argument: 'num'

@Clunk_Head the assignment before was a simple calculator without class
Reply
#6
(Dec-04-2019, 09:08 PM)Spacecowboy Wrote: ##UPDATE##

@ line 47 changed to : added_value = Rekenmachine

There is now an error
   
btn[i] ["command"] = lambda x= numberpad[i]: added_value.numberEnter(x)
TypeError: numberEnter() missing 1 required positional argument: 'num'

@Clunk_Head the assignment before was a simple calculator without class
try
btn[i] ["command"] = lambda: added_value.numberEnter(numberpad[i])
but if you don't need a class in the program then you can simplify your code quite a bit by removing it, turning the instance variables into global variables, and defining the functions independent of a class.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  i need help in fixing my scientific calculator coding : (, im using python 3.5.5 hans77 1 4,150 Oct-17-2018, 03:26 AM
Last Post: stullis
  scientific numbers metalray 1 2,449 Feb-06-2018, 06:50 PM
Last Post: boring_accountant

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020