Advertisement
Guest User

Untitled

a guest
May 26th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. from tkinter import *
  2. import math
  3.  
  4. class calc:
  5. def getandreplace(self):
  6. #заменяем * знаком x и / знаком ÷
  7. self.expression = self.e.get()
  8. self.newtext=self.expression.replace('/','/')
  9. self.newtext=self.newtext.replace('x','*')
  10. def equals(self):
  11. #когда нажат знак "равно"
  12. self.getandreplace()
  13. try:
  14. #вычисление результата с помощью math
  15. self.value= eval(self.newtext)
  16. except SyntaxError or NameError:
  17. self.e.delete(0,END)
  18. self.e.insert(0,'Неправильный ввод')
  19. else:
  20. self.e.delete(0,END)
  21. self.e.insert(0,self.value)
  22. def squareroot(self):
  23. #вычисление квадратного корня
  24. self.getandreplace()
  25. try:
  26. #вычисление результата с помощью math
  27. self.value= eval(self.newtext)
  28. except SyntaxError or NameError:
  29. self.e.delete(0,END)
  30. self.e.insert(0,'Некорректный ввод!')
  31. else:
  32. self.sqrtval=math.sqrt(self.value)
  33. self.e.delete(0,END)
  34. self.e.insert(0,self.sqrtval)
  35. def square(self):
  36. #вычисление квадрата числа
  37. self.getandreplace()
  38. try:
  39. #вычисление результата с помощью math
  40. self.value= eval(self.newtext)
  41. except SyntaxError or NameError:
  42. self.e.delete(0,END)
  43. self.e.insert(0,'Некорректный ввод!')
  44. else:
  45. self.sqval=math.pow(self.value,2)
  46. self.e.delete(0,END)
  47. self.e.insert(0,self.sqval)
  48. def clearall(self):
  49. #при нажатии удаляется всё из поля ввода
  50. self.e.delete(0,END)
  51. def clear1(self):
  52. self.txt=self.e.get()[:-1]
  53. self.e.delete(0,END)
  54. self.e.insert(0,self.txt)
  55. def action(self,argi):
  56. """pressed button's value is inserted into the end of the text area"""
  57. self.e.insert(END,argi)
  58. def __init__(self,master):
  59. """Constructor method"""
  60. master.title('Калькулятор')
  61. master.geometry()
  62. self.e = Entry(master)
  63. self.e.grid(row=0,column=0,columnspan=6,pady=3)
  64. self.e.focus_set() #устновим фокус на поле ввода
  65.  
  66. # Создание кнопок
  67. Button(master,text="=",width=11,height=3,fg="blue",
  68. bg="red",command=lambda:self.equals()).grid(
  69. row=4, column=4,columnspan=2)
  70.  
  71. Button(master,text='AC',width=5,height=3, bg="gray",
  72. command=lambda:self.clearall()).grid(row=1, column=4)
  73.  
  74. Button(master,text='C',width=5,height=3,bg="grey",
  75. command=lambda:self.clear1()).grid(row=1, column=5)
  76.  
  77. Button(master,text="+",width=5,height=3,bg="light green",
  78. command=lambda:self.action('+')).grid(row=4, column=3)
  79.  
  80. Button(master,text="x",width=5,height=3,bg="light green",
  81. command=lambda:self.action('x')).grid(row=2, column=3)
  82.  
  83. Button(master,text="-",width=5,height=3,bg="light green",
  84. command=lambda:self.action('-')).grid(row=3, column=3)
  85.  
  86. Button(master,text="÷",width=5,height=3,bg="light green",
  87. command=lambda:self.action('/')).grid(row=1, column=3)
  88.  
  89. Button(master,text="%",width=5,height=3,bg="light green",
  90. command=lambda:self.action('%')).grid(row=4, column=2)
  91.  
  92. Button(master,text="7",width=5,height=3,bg="light blue",
  93. command=lambda:self.action('7')).grid(row=1, column=0)
  94.  
  95. Button(master,text="8",width=5,height=3,bg="light blue",
  96. command=lambda:self.action(8)).grid(row=1, column=1)
  97.  
  98. Button(master,text="9",width=5,height=3,bg="light blue",
  99. command=lambda:self.action(9)).grid(row=1, column=2)
  100.  
  101. Button(master,text="4",width=5,height=3,bg="light blue",
  102. command=lambda:self.action(4)).grid(row=2, column=0)
  103.  
  104. Button(master,text="5",width=5,height=3,bg="light blue",
  105. command=lambda:self.action(5)).grid(row=2, column=1)
  106.  
  107. Button(master,text="6",width=5,height=3,bg="light blue",
  108. command=lambda:self.action(6)).grid(row=2, column=2)
  109.  
  110. Button(master,text="1",width=5,height=3,bg="light blue",
  111. command=lambda:self.action(1)).grid(row=3, column=0)
  112.  
  113. Button(master,text="2",width=5,height=3,bg="light blue",
  114. command=lambda:self.action(2)).grid(row=3, column=1)
  115.  
  116. Button(master,text="3",width=5,height=3,bg="light blue",
  117. command=lambda:self.action(3)).grid(row=3, column=2)
  118.  
  119. Button(master,text="0",width=5,height=3,bg="light blue",
  120. command=lambda:self.action(0)).grid(row=4, column=0)
  121.  
  122. Button(master,text=".",width=5,height=3,bg="light blue",
  123. command=lambda:self.action('.')).grid(row=4, column=1)
  124.  
  125. Button(master,text="(",width=5,height=3,bg="orange",
  126. command=lambda:self.action('(')).grid(row=2, column=4)
  127.  
  128. Button(master,text=")",width=5,height=3,bg="orange",
  129. command=lambda:self.action(')')).grid(row=2, column=5)
  130.  
  131. Button(master,text="√",width=5,height=3,bg="orange",
  132. command=lambda:self.squareroot()).grid(row=3, column=4)
  133.  
  134. Button(master,text="x²",width=5,height=3,bg="orange",
  135. command=lambda:self.square()).grid(row=3, column=5)
  136.  
  137. # Driver Code
  138. root = Tk()
  139.  
  140. obj=calc(root) # object instantiated
  141.  
  142. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement