Solved: python calculator source code

The main problem with python calculator source code is that it can be difficult to read and understand.


#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ File: calculator.py Description: A simple calculator program. Author: William C. Gunnells License: MIT License """ from tkinter import * class Application(Frame): def __init__(self, master): super(Application, self).__init__(master) self.grid() self.create_widgets() def create_widgets(self): # create instruction label self.inst_lbl = Label(self, text = "Enter calculation below") self.inst_lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W) # create a label and text entry box for the first number self.num1_lbl = Label(self, text="First Number:") self.num1_lbl.grid(row=1, column=0, sticky=W) self.num1_ent = Entry(self) self.num1_ent.grid(row=1, column=1, sticky=W) # create a label and text entry box for the second number self.num2_lbl = Label(self, text="Second Number:") self.num2_lbl.grid(row=2, column=0, sticky=W) self.num2_ent = Entry(self) self.num2_ent.grid(row=2, column=1, sticky=W) # create a submit button self.submit_bttn = Button(self, text="Submit", command = self.reveal) self.submit_bttn.grid(row=3, column=0, sticky=W) # create a text box to display the result self.result_txt = Text(self, width = 35, height = 5, wrap = WORD) self.result_txt.grid(row=4, column=0, columnspan = 2, sticky=W) def reveal(self): # retrieve the contents of the entry boxes num1 = float(self.num1_ent.get()) num2 = float(self.num2_ent .get()) # calculate the result of the two numbers addition operation result = num1 + num2 # insert the result into the output text widget self .result _txt .delete ( 0 . 0 , END ) self .result _txt .insert ( 0 . 0 , result ) root = Tk() root["bg"]="light blue" root["padx"]="5" root["pady"]="5" app = Application (root ) app ["bg"]="light blue" app ["padx"]="5" app ["pady"]="#5" root .mainloop ()

#!/usr/bin/env python3 - this line tells the computer what program to use to run this code
# -*- coding: utf-8 -*- - this line sets the encoding for this document
""" File: calculator.py Description: A simple calculator program Author: William C License: MIT License """ - these lines provide documentation for this program
from tkinter import * - this line imports the tkinter module
class Application (Frame ): def __init__ (self , master ): super (Application , self ). __init__ (master ) self . grid () self . create _widgets () def create _widgets (self ): # create instruction label - these lines define a class called Application that inherits from Frame; within this class is a method to initialize it and a method to create widgets; this comment explains what the following lines do
self . inst _lbl Label ( sel f , text "Enter calculation below") sel f inst _lbl grid row 0 , column 0 , columnspan 2 , sticky W #create a label and text entry box for first number-this line creates a label widget with the text "First Number:" and positions it in row 1, column 0; it also creates an entry widget and positions is in row 1, column 1 next to the label widget; both widgets are stuck to the west side of their respective cells
sel f num1 _lbl Label sel f te x t "First Number:" sel f num1 _lb l gri d ro w 1 co lu mn 0 sti c ky W se lf n um1 e nt E ntry s elf nu m1 e nt gri d ro w 1 c ol umn 1 sti c ky W#create submit button-this line creates a button widget with the text "Submit" and attaches it to a command that will execute when it is clicked; it then positions is in row 3column 0 sticking it to west side of its cell
sel f subm i t bttn Bu tton s elf te x t "Submi t " com mand s elf rev ea l se lf subm i t bttn gri d ro w 3 col umn 0 sti c ky W#create output text box-this line creates a text widget that will be used to display our results; we set its width and height in characters and tell it not wrap words; we then position is in row 4 spanning columns 0 & 1 sticking it ot he west side of its cell

Calculator

A calculator is a program that performs arithmetic, logical, and trigonometric operations on numbers. They are widely used in mathematics and other sciences.

Very Simple Calculator

A very simple calculator in Python can be implemented as follows:

from math import * def calc(x): return x*x

Related posts:

Leave a Comment