![]() |
Help adding memory buttons/commands in the calculator - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help adding memory buttons/commands in the calculator (/thread-32766.html) |
Help adding memory buttons/commands in the calculator - joelpub - Mar-04-2021 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() RE: Help adding three buttons/commands in the calculator - deanhystad - Mar-04-2021 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? RE: Help adding three buttons/commands in the calculator - joelpub - Mar-04-2021 (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 RE: Help adding memory buttons/commands in the calculator - deanhystad - Mar-04-2021 "Ms : Save number to memory." What number? Can you point out in your code what number you want to save? RE: Help adding memory buttons/commands in the calculator - joelpub - Mar-04-2021 (Mar-04-2021, 06:52 PM)deanhystad Wrote: "Ms : Save number to memory." 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. RE: Help adding memory buttons/commands in the calculator - deanhystad - Mar-04-2021 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. |