Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Wondering if someone is familiar with tkinter and functools modules in Python, to complete several functions here and here is my unfinished codes: Add the

Wondering if someone is familiar with tkinter and functools modules in Python, to complete several functions here and here is my unfinished codes:

Add the following to the calculator:

  • Subtraction
  • Multiplication
  • User interface must be intuitive! Use the built in calculator in your OS as an example

Add one of the following :

  • -/+button
  • This will allow the user to change the current number to the opposite sign
  • Negative will become positive
  • Positive will become negative
  • .button (that is a decimal point)
  • This will allow the user to enter decimals

Below is my codes:(unfinished)

import tkinter as tk

import functools

class Calculator:

def __init__(self):

self._numbers = None

self._operation = None # You'll add the multiplication

self._result = None

self.clear_numbers()

def update_number(self, number, update_reason):

# update_reason : +, *, /, -

self._numbers.append(number)

self._operation = update_reason

print(self._numbers, self._operation, sep=" : ")

def multiplication(self, number):

pass

def division(self, number):

pass

def equal_operation(self, number):

self._result = self._numbers[-1] + number

def get_result(self):

return self._result

def clear_numbers(self):

self._numbers = [0]

self._result = 0

class CalculatorGUI:

'''

Responsible for creating the calculator GUI

'''

def __init__(self, root):

self._backend = Calculator()

root.title("Calculator")

# The entry, where the numbers will appear

self._entry = tk.Entry(root, width=40, borderwidth=5)

self._entry.grid(row=0, column=0, columnspan=3,

padx=10, pady=10)

# Buttons

self._buttons = dict()

buttons = list(range(9,0,-1))

buttons.extend(["=", "+", "x", "/",0])

c = 2

r = 1

for i in buttons:

if i in range(0,10,1):

button_function = functools.partial(self.button_number, i)

elif i == "=":

button_function = self.button_equal

elif i == "x":

button_function = self.button_multiplication

elif i == "/":

button_function = self.button_division

elif i == "+":

button_function = self.button_plus

temp = tk.Button(root, text=i, padx=40, pady=20,

command=button_function)

temp.grid(row=r, column=c)

self._buttons[i] = temp

c -= 1

if c == -1:

r += 1

c = 2

# Clear button

self._buttons['Clear'] = tk.Button(root, text="Clear",

padx=110, pady=20,

command=self.button_clear)

self._buttons['Clear'].grid(row=r+1, column=0, columnspan=3)

def button_number(self, text):

current = self._entry.get()

self._entry.delete(0, tk.END)

self._entry.insert(0, str(current) + str(text))

def button_equal(self):

self._backend.equal_operation(int(self._entry.get()))

self._entry.delete(0, tk.END)

self._entry.insert(0, self._backend.get_result())

def button_plus(self):

self._backend.update_number(int(self._entry.get()), "+")

self._entry.delete(0, tk.END)

def button_multiplication(self):

self._backend.update_number(int(self._entry.get()), "x")

self._entry.delete(0, tk.END)

def button_division(self):

self._backend.update_number(int(self._entry.get()), "/")

self._entry.delete(0, tk.END)

def button_clear(self):

self._entry.delete(0, tk.END)

if __name__ == "__main__":

root = tk.Tk()

app = CalculatorGUI(root)

root.mainloop()

Programming language: Python

Requirement: only need to complete several functions as demonstrated above.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

More Books

Students also viewed these Programming questions

Question

Explain the importance of Physical distribution.

Answered: 1 week ago

Question

Define Marketing research.

Answered: 1 week ago

Question

1. What is blood circulation? 2. Three types of blood vessels?

Answered: 1 week ago

Question

Why are red blood cells Red in colour?

Answered: 1 week ago