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


Messages In This Thread
Help adding memory buttons/commands in the calculator - by joelpub - Mar-04-2021, 03:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Made a discord bot game that uses commands but now trying to add buttons Dmcsdiscord 1 243 Apr-22-2024, 02:27 PM
Last Post: Dmcsdiscord
  Adding markers to Folium map only adding last element. tantony 0 2,152 Oct-16-2019, 03:28 PM
Last Post: tantony
  AttributeError: 'Calculator' object has no attribute 'buttons' Gomez2021 5 6,348 Apr-18-2018, 01:34 AM
Last Post: IAMK
  Buttons or Radio Buttons on Tkinter Treeview draems 0 3,415 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