How to add the memory buttons commands in this calculator?

Good night. Does anyone would help me to add 3 more buttons (memory: MS, M+ and M-) commands to this calculator? I would appreciate so much! I have tried for hours and got nothing :frowning:
It is very important to me. Thank you!!!

#### Buttons I don’t know how to make it work
Button(width=4, text=‘Ms’, relief=‘flat’, command=self.memo_add).place(x=360, y=50)
Button(width=4, text=‘M+’, relief=‘flat’, command=self.memo_sum).place(x=360, y=90)
Button(width=4, text=‘M-’, relief=‘flat’, command=self.memo_sub).place(x=360, y=130)

def memo_add(self): # must save number to memory

def memo_sum(self): # must sum the saved number to the screen value

def memo_sub(self): # must subtract from the screen value

from tkinter import Tk, Entry, Button, StringVar

class Calculadora:
    def __init__(self, base_calculadora):
        self.memory = 0

        self.equation = StringVar()
        self.entry_value = ''
        Entry(width=36, bg='lightblue', font=('Verdana', 16), textvariable=self.equation).place(x=0, y=0)

        Button(width=8, text='(', relief='flat', command=lambda:'(')).place(x=0, y=50)
        Button(width=8, text=')', relief='flat', command=lambda:')')).place(x=90, y=50)
        Button(width=8, text='%', relief='flat', command=lambda:'%')).place(x=180, y=50)
        Button(width=8, text='1', relief='flat', command=lambda:, y=90)
        Button(width=8, text='2', relief='flat', command=lambda:, y=90)
        Button(width=8, text='3', relief='flat', command=lambda:, y=90)
        Button(width=8, text='4', relief='flat', command=lambda:, y=130)
        Button(width=8, text='5', relief='flat', command=lambda:, y=130)
        Button(width=8, text='6', relief='flat', command=lambda:, y=130)
        Button(width=8, text='7', relief='flat', command=lambda:, y=170)
        Button(width=8, text='8', relief='flat', command=lambda:, y=170)
        Button(width=8, text='9', relief='flat', command=lambda:, y=170)
        Button(width=8, text='0', relief='flat', command=lambda:, y=210)
        Button(width=8, text='.', relief='flat', command=lambda:'.')).place(x=90, y=210)
        Button(width=8, text='+', relief='flat', command=lambda:'+')).place(x=270, y=90)
        Button(width=8, text='-', relief='flat', command=lambda:'-')).place(x=270, y=130)
        Button(width=8, text='/', relief='flat', command=lambda:'/')).place(x=270, y=170)
        Button(width=8, text='x', relief='flat', command=lambda:'*')).place(x=270, y=210)
        Button(width=8, text='=', bg='green', relief='flat', command=self.solve).place(x=180, y=210)
        Button(width=8, text='AC', relief='flat', command=self.clear).place(x=270, y=50)

        ########### Buttons I don't know how to make it work
        Button(width=4, text='Ms', relief='flat', command=self.memo_add).place(x=360, y=50)  # must save to memory
        Button(width=4, text='M+', relief='flat', command=self.memo_sum).place(x=360, y=90)  # must sum to the screen value
        Button(width=4, text='M-', relief='flat', command=self.memo_sub).place(x=360, y=130)   # must subtract from the screen value

    #### COMMANDS
    def memo_add(self):

    def memo_sum(self):

    def memo_sub(self):

    def show(self, value):
        self.entry_value += str(value)

    def clear(self):
        self.entry_value = ''

    def solve(self):
        result = eval(self.entry_value)

root = Tk()
calculator = Calculadora(root)
1 Like

Well, it seems you would know how to do that. It does not differ from the other buttons you created.

The command of course differs; you don’t need to show something, but you need to store. Define a “memory location”, I think you already did that, MS stores to memory (store_to_memory offers itself as the name), M+ adds to memory and M- subtracts from memory.

I’ll leave the implementation to you.

Hi, Menno. Thanks for the tip. I have created the buttons, tried for hours to write the commands, but I was not capable of making it work. If you could help me with more details it would be nice. Thank you again.

I edited the post to the new code.

I would start by extending your “if ladder” to include the use cases you
intend to handle, such as:

  • Memory clear (mc)
  • Memory plus (m+)
  • Memory minus (m-)
  • Memory recall (mr)

and write in English (ou em Português, simplesmente “em prosa”) what you want
to happen for those cases.

The need for a variable to store the memorised value will naturally emerge from
the previous reasoning and the exercise will turn into expressing in code what
you defined precisely earlier.

You could also write down a sequence of operations from an imaginary user and
the expected state of your memorised value at each step and then test your
program to check that it behaves as prescribed, fixing it where it diverges
from what you anticipated.

Boa sorte!

  1. When you mean “if ladder” is before that ‘break’ thing?

This if ... elif ... elif ... else construct sometimes goes by the informal
name of “if ladder”, see an example of it here:

  1. I think i understand your point, but im stucked in how im go to set the
    functions to make it. Using def memory (MS,M+,M-…)?

Defining Functions is explained in the Python Tutorial here:

  1. where do i put this?

This answer is up to you really but it might be easiest to just add a “clause”
to your existing if ... elif logic.

“Clause” is terminology explained in the same page as 1) above.