Python Forum
Help adding memory buttons/commands in the calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help adding memory buttons/commands in the calculator
#1
Good morning. I have a calculator and I need to make the buttons MS (save to memory), M+ (sum what's saved in memory to the actual screen value) and M- (subtract...), like the real calculators. Does anyone would help me creating the COMMAND to do so? PLEASE! I would appreciate so much. I have been working for hours and got nothing. Thank you very much.

from tkinter import Tk, Entry, Button, StringVar


class Calculadora:
    def __init__(self, base_calculadora):
        base_calculadora.title('Calculadora')
        base_calculadora.geometry('403x260+0+0')
        self.memory = 0
        base_calculadora.config(bg='#438')
        base_calculadora.resizable()

        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: self.show('(')).place(x=0, y=50)
        Button(width=8, text=')', relief='flat', command=lambda: self.show(')')).place(x=90, y=50)
        Button(width=8, text='%', relief='flat', command=lambda: self.show('%')).place(x=180, y=50)
        Button(width=8, text='1', relief='flat', command=lambda: self.show(1)).place(x=0, y=90)
        Button(width=8, text='2', relief='flat', command=lambda: self.show(2)).place(x=90, y=90)
        Button(width=8, text='3', relief='flat', command=lambda: self.show(3)).place(x=180, y=90)
        Button(width=8, text='4', relief='flat', command=lambda: self.show(4)).place(x=0, y=130)
        Button(width=8, text='5', relief='flat', command=lambda: self.show(5)).place(x=90, y=130)
        Button(width=8, text='6', relief='flat', command=lambda: self.show(6)).place(x=180, y=130)
        Button(width=8, text='7', relief='flat', command=lambda: self.show(7)).place(x=0, y=170)
        Button(width=8, text='8', relief='flat', command=lambda: self.show(8)).place(x=180, y=170)
        Button(width=8, text='9', relief='flat', command=lambda: self.show(9)).place(x=90, y=170)
        Button(width=8, text='0', relief='flat', command=lambda: self.show(0)).place(x=0, y=210)
        Button(width=8, text='.', relief='flat', command=lambda: self.show('.')).place(x=90, y=210)
        Button(width=8, text='+', relief='flat', command=lambda: self.show('+')).place(x=270, y=90)
        Button(width=8, text='-', relief='flat', command=lambda: self.show('-')).place(x=270, y=130)
        Button(width=8, text='/', relief='flat', command=lambda: self.show('/')).place(x=270, y=170)
        Button(width=8, text='x', relief='flat', command=lambda: self.show('*')).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 I NEED HELP WITH
    def memo_add(self):

    def memo_sum(self):

    def memo_sub(self):


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

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

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


root = Tk()
calculator = Calculadora(root)
root.mainloop()
Reply
#2
What should they do? memo suggests that there is a place to take a memo, some kind of storage location. What do you want to happen when memo_add() is called?
Reply
#3
(Mar-04-2021, 04:24 PM)deanhystad Wrote: What should they do? memo suggests that there is a place to take a memo, some kind of storage location. What do you want to happen when memo_add() is called?

Dean, there are three buttons:
MS: save number to memory
M+: sum the memory saved number to the number on the screen
M-: subtract the memory saved number from the number on the screen
Like a real calculator.

#### 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 I NEED HELP WITH
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
Reply
#4
"Ms : Save number to memory."

What number? Can you point out in your code what number you want to save?
Reply
#5
(Mar-04-2021, 06:52 PM)deanhystad Wrote: "Ms : Save number to memory."

What number? Can you point out in your code what number you want to save?

Dean, thanks for trying to help.

It is a calculator. So, I'll press, for example, the buttons 4+4 and = . The result will be 8, right? I want to record the result (whatever it is) to the memory to use later. I'll press the MS button to save the result (8) to memory. Was I clear?

If you try in a real calculator, the MS button save in memory the value that is being shown in the screen and put it back when press M+ (adding) or M- (subtracting). Understand?

Thank you again.
Reply
#6
I see a variable named memory which I expect is going to be the memory store. Where is the value that should get copied to memory? Is that "result" in the solve() method? Currently there's no way to get result from outside the solve() method. Guess you'll have to fix that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding markers to Folium map only adding last element. tantony 0 844 Oct-16-2019, 03:28 PM
Last Post: tantony
  AttributeError: 'Calculator' object has no attribute 'buttons' Gomez2021 5 3,170 Apr-18-2018, 01:34 AM
Last Post: IAMK
  Buttons or Radio Buttons on Tkinter Treeview draems 0 1,841 Oct-31-2017, 04:06 AM
Last Post: draems

Forum Jump:

User Panel Messages

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