Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # PyCalc, a small Pyhton calculator.
- # Verison 0.3x
- # Created by Ceil
- # Created on May 15, 2017 @ 10:30pm
- # Performs basic math functions (add, subtract, multiply, divide).
- # Does NOT support direct equations in the text bar... yet ;)
- # This is my first GUI Python application so feel free to shoot me any pofloaters!
- # Code is open source.
- import math
- import os
- from Tkinter import *
- mathType = 0
- firstNumber = 0
- # Functions here to put numbers in the textbox
- def B1m():
- text.insert(INSERT, "1")
- return
- def B2m():
- text.insert(INSERT, "2")
- return
- def B3m():
- text.insert(INSERT, "3")
- return
- def B4m():
- text.insert(INSERT, "4")
- return
- def B5m():
- text.insert(INSERT, "5")
- return
- def B6m():
- text.insert(INSERT, "6")
- return
- def B7m():
- text.insert(INSERT, "7")
- return
- def B8m():
- text.insert(INSERT, "8")
- return
- def B9m():
- text.insert(INSERT, "9")
- return
- def B0m():
- text.insert(INSERT, "0")
- return
- # This function handes the Clear button
- def Bclear():
- text.delete(1.0, END)
- return
- # These functions handle basic math and decimal point
- def Badd():
- global mathType
- mathType = 1
- global firstNumber
- firstNumber = text.get('1.0', 'end-1c')
- text.delete(1.0, END)
- return
- def Bminus():
- global mathType
- mathType = 2
- global firstNumber
- firstNumber = text.get('1.0', 'end-1c')
- text.delete(1.0, END)
- return
- def Btimes():
- global mathType
- mathType = 3
- global firstNumber
- firstNumber = text.get('1.0', 'end-1c')
- text.delete(1.0, END)
- return
- def Bdiv():
- global mathType
- mathType = 4
- global firstNumber
- firstNumber = text.get('1.0', 'end-1c')
- text.delete(1.0, END)
- return
- def dPoint():
- text.insert(INSERT, ".")
- return
- def Bequal():
- if mathType == 1:
- result = float(firstNumber) + float(text.get('1.0', 'end-1c'))
- elif mathType == 2:
- result = float(firstNumber) - float(text.get('1.0', 'end-1c'))
- elif mathType == 3:
- result = float(firstNumber) * float(text.get('1.0', 'end-1c'))
- elif mathType == 4:
- result = float(firstNumber) / float(text.get('1.0', 'end-1c'))
- text.delete(1.0, END)
- text.insert(INSERT, result)
- return
- # Function to display and position buttons
- def buttons (bid,txt,command,color,pos1,pos2):
- if os.name == 'nt':
- pass
- bid = Button(root, text =txt, command=command,fg=color,width=2,height=1)
- else:
- pass
- bid = Button(root, text =txt, command=command,fg=color)
- bid.place(x=pos1, y=pos2)
- # The next 4 lines create the window, set a title, set the window size and make sure we don't allow resize!
- root = Tk()
- root.title("PyCalc!") # Window title
- root.resizable(width=False, height=False) # Makes so the window can't be resized
- root.geometry("265x400") # Set the window width X height
- # Following 3 lines create and place the text window
- text = Text(root,height=-100)
- text.insert(INSERT, "")
- text.place(x=0, y=0)
- # The following ten methods print out the basic numbers
- buttons("b1","1",B1m,"blue",10,50)
- buttons("b2","2",B2m,"blue",50,50)
- buttons("b3","3",B3m,"blue",90,50)
- buttons("b4","4",B4m,"blue",130,50)
- buttons("b5","5",B5m,"blue",170,50)
- buttons("b6","6",B6m,"blue",10,100)
- buttons("b7","7",B7m,"blue",50,100)
- buttons("b8","8",B8m,"blue",90,100)
- buttons("b9","9",B9m,"blue",130,100)
- buttons("b0","0",B0m,"blue",170,100)
- # Print out the clear key
- buttons("bclear","C",Bclear,"red",210,50)
- # Basic math methods and the decimal point
- buttons("badd","+",Badd,"red",10,150)
- buttons("bminus","-",Bminus,"red",50,150)
- buttons("btimes","*",Btimes,"red",90,150)
- buttons("bdiv","/",Bdiv,"red",130,150)
- buttons("bequal","=",Bequal,"red",170,150)
- buttons("decimalPoint",".",dPoint,"red",210,100)
- root.mainloop() # LooooOOOoop!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement