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!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
1
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
1
2
3
    
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
1
2
3
    
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
1
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,973 Oct-17-2018, 03:26 AM
Last Post: stullis
  scientific numbers metalray 1 3,151 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