• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
24%
SHARE
TWEET

# Python Basic Calculator

a guest May 19th, 2017 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #!/usr/bin/python
2.
3. # PyCalc, a small Pyhton calculator.
4. # Verison 0.3x
5. # Created by Ceil
6. # Created on May 15, 2017 @ 10:30pm
7. # Performs basic math functions (add, subtract, multiply, divide).
8.  # Does NOT support direct equations in the text bar... yet ;)
9.  # This is my first GUI Python application so feel free to shoot me any pofloaters!
10.  # Code is open source.
11.
12. import math
13. import os
14. from Tkinter import *
15. mathType = 0
16. firstNumber = 0
17.
18. # Functions here to put numbers in the textbox
19. def B1m():
20.     text.insert(INSERT, "1")
21.     return
22. def B2m():
23.     text.insert(INSERT, "2")
24.     return
25. def B3m():
26.     text.insert(INSERT, "3")
27.     return
28. def B4m():
29.     text.insert(INSERT, "4")
30.     return
31. def B5m():
32.     text.insert(INSERT, "5")
33.     return
34. def B6m():
35.     text.insert(INSERT, "6")
36.     return
37. def B7m():
38.     text.insert(INSERT, "7")
39.     return
40. def B8m():
41.     text.insert(INSERT, "8")
42.     return
43. def B9m():
44.     text.insert(INSERT, "9")
45.     return
46. def B0m():
47.     text.insert(INSERT, "0")
48.     return
49. # This function handes the Clear button
50. def Bclear():
51.     text.delete(1.0, END)
52.     return
53. # These functions handle basic math and decimal point
55.     global  mathType
56.     mathType = 1
57.     global  firstNumber
58.     firstNumber = text.get('1.0', 'end-1c')
59.     text.delete(1.0, END)
60.     return
61. def Bminus():
62.     global  mathType
63.     mathType = 2
64.     global  firstNumber
65.     firstNumber = text.get('1.0', 'end-1c')
66.     text.delete(1.0, END)
67.     return
68. def Btimes():
69.     global  mathType
70.     mathType = 3
71.     global  firstNumber
72.     firstNumber = text.get('1.0', 'end-1c')
73.     text.delete(1.0, END)
74.     return
75. def Bdiv():
76.     global  mathType
77.     mathType = 4
78.     global  firstNumber
79.     firstNumber = text.get('1.0', 'end-1c')
80.     text.delete(1.0, END)
81.     return
82. def dPoint():
83.     text.insert(INSERT, ".")
84.     return
85. def Bequal():
86.     if mathType == 1:
87.         result = float(firstNumber) + float(text.get('1.0', 'end-1c'))
88.     elif mathType == 2:
89.         result = float(firstNumber) - float(text.get('1.0', 'end-1c'))
90.     elif mathType == 3:
91.         result = float(firstNumber) * float(text.get('1.0', 'end-1c'))
92.     elif mathType == 4:
93.         result = float(firstNumber) / float(text.get('1.0', 'end-1c'))
94.     text.delete(1.0, END)
95.     text.insert(INSERT, result)
96.     return
97. # Function to display and position buttons
98. def buttons (bid,txt,command,color,pos1,pos2):
99.     if os.name == 'nt':
100.         pass
101.         bid = Button(root, text =txt, command=command,fg=color,width=2,height=1)
102.     else:
103.         pass
104.         bid = Button(root, text =txt, command=command,fg=color)
105.     bid.place(x=pos1, y=pos2)
106.
107. # The next 4 lines create the window, set a title, set the window size and make sure we don't allow resize!
108. root = Tk()
109. root.title("PyCalc!")   # Window title
110. root.resizable(width=False, height=False)   # Makes so the window can't be resized
111. root.geometry("265x400")   # Set the window width X height
112.
113. # Following 3 lines create and place the text window
114. text = Text(root,height=-100)
115. text.insert(INSERT, "")
116. text.place(x=0, y=0)
117.
118. # The following ten methods print out the basic numbers
119. buttons("b1","1",B1m,"blue",10,50)
120. buttons("b2","2",B2m,"blue",50,50)
121. buttons("b3","3",B3m,"blue",90,50)
122. buttons("b4","4",B4m,"blue",130,50)
123. buttons("b5","5",B5m,"blue",170,50)
124. buttons("b6","6",B6m,"blue",10,100)
125. buttons("b7","7",B7m,"blue",50,100)
126. buttons("b8","8",B8m,"blue",90,100)
127. buttons("b9","9",B9m,"blue",130,100)
128. buttons("b0","0",B0m,"blue",170,100)
129.
130. # Print out the clear key
131. buttons("bclear","C",Bclear,"red",210,50)
132.
133. # Basic math methods and the decimal point