fork download
  1. import re
  2. from tkinter import *
  3. from math import sqrt
  4. class Calc():
  5. s = ''
  6. def add_it(n):
  7. screen.insert(END, n)
  8. Calc.s+=n
  9. def bcksp():
  10. screen.delete(len(Calc.s)-1, END)
  11. Calc.s = Calc.s[:len(Calc.s)-1]
  12. def ssqrt():
  13. try:
  14. screen.delete(0,END)
  15. sq = round(sqrt(float(Calc.s)),3)
  16. if re.match(r'\d+\.{1}0{1}\b',str(sq)):
  17. sq = re.sub(r'(\d+)(\.0)',r'\1', str(sq))
  18. screen.insert(END,sq)
  19. Calc.s = str(float(screen.get()))
  20. except:
  21. Calc.s = ''
  22. screen.insert(END, ' ERROR ')
  23. def equ():
  24. try:
  25. x = float(re.findall(r'\-*\d+\.*\d*',Calc.s)[0])
  26. y = float(re.findall(r'\d+\.*\d*',Calc.s)[1])
  27. act = re.search(r'[\^+*/-]{1}', Calc.s[1:]).group()
  28. if act == '+':
  29. z = round(x+y,2)
  30. elif act == '-':
  31. z = round(x-y,2)
  32. elif act == '*':
  33. z = round(x*y,2)
  34. elif act == '^':
  35. z = round(x**y,2)
  36. else:
  37. z = round(x/y,2)
  38. if re.match(r'\d+\.{1}0{1}\b',str(z)):
  39. z = re.sub(r'(\d+)(\.0)',r'\1', str(z))
  40. screen.delete(0,END)
  41. screen.insert(END,z)
  42. Calc.s = str(z)
  43. except:
  44. Calc.s = ''
  45. screen.insert(END, ' ERROR ')
  46. def clear():
  47. screen.delete(0,END)
  48. Calc.s=''
  49.  
  50. window = Tk()
  51. window.resizable(0,0)
  52. window.geometry('272x308')
  53. window.title('Simple Calculator')
  54. screen = Entry(window, bd=8, font = 3)
  55. one = Button(window, text='1',height=2,width=6,bd=4,bg='azure',font=14, command=lambda event='<Button-1>' : Calc.add_it('1'))
  56. two = Button(window, text='2',height=2,width=6,bd=4,bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('2'))
  57. three = Button(window, text='3',height=2,width=6,bd=4,bg='azure',font=14, command=lambda event='<Button-1>' : Calc.add_it('3'))
  58. four = Button(window, text='4',height=2,width=6,bd=4, bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('4'))
  59. five = Button(window, text='5',height=2,width=6, bd=4,bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('5'))
  60. six = Button(window, text='6',height=2,width=6, bd=4,bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('6'))
  61. seven = Button(window, text='7',height=2,width=6,bd=4,bg='azure',font=14, command=lambda event='<Button-1>' : Calc.add_it('7'))
  62. eight = Button(window, text='8',height=2,width=6,bd=4,bg='azure',font=14, command=lambda event='<Button-1>' : Calc.add_it('8'))
  63. nine = Button(window, text='9',height=2,width=6, bd=4,bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('9'))
  64. zero = Button(window, text='0',height=2,width=6,bd=4, bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('0'))
  65. plus = Button(window, text='+',height=2,width=6,bd=4,bg='azure',font=14, command=lambda event='<Button-1>' : Calc.add_it('+'))
  66. minus = Button(window, text='-',height=2,width=6,bd=4,bg='azure',font=14, command=lambda event='<Button-1>' : Calc.add_it('-'))
  67. div = Button(window, text='/',height=2,width=6,bd=4, bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('/'))
  68. mult = Button(window, text='*',height=2,width=6, bd=4,bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('*'))
  69. equ = Button(window, text='=',height=2,width=6,bd=4, bg='azure',font=14,command=Calc.equ)
  70. clear = Button(window, text='C',height=2,width=6,bd=4, bg='azure',font=14,fg='red',command=Calc.clear)
  71. bs = Button(window, text='<<',height=2,width=6,bd=4, bg='azure',font=14,fg='green', command= Calc.bcksp)
  72. dot = Button(window, text='.',height=2,width=6,bd=4, bg='azure',font=14,fg='orange',command=lambda event='<Button-1>':Calc.add_it('.'))
  73. sqr = Button(window, text='sq',height=2,width=6,bd=4, bg='azure',font=14,fg='orange',command=Calc.ssqrt)
  74. exp = Button(window, text='^',height=2,width=6, bd=4,bg='azure',font=14,command=lambda event='<Button-1>' : Calc.add_it('^'))
  75.  
  76. screen.grid(row=0,rowspan=2,columnspan=12)
  77. exp.grid(row=2,column=0)
  78. sqr.grid(row=2,column=1)
  79. bs.grid(row=2,column=2)
  80. clear.grid(row=2,column=3)
  81. plus.grid(row=3,column=0)
  82. minus.grid(row=3,column=1)
  83. mult.grid(row=3,column=2)
  84. div.grid(row=3,column=3)
  85. one.grid(row=4,column=0)
  86. two.grid(row=4,column=1)
  87. three.grid(row=4,column=2)
  88. four.grid(row=4,column=3)
  89. five.grid(row=5,column=0)
  90. six.grid(row=5,column=1)
  91. seven.grid(row=5,column=2)
  92. eight.grid(row=5,column=3)
  93. nine.grid(row=6,column=0)
  94. zero.grid(row=6,column=1)
  95. dot.grid(row=6,column=2)
  96. equ.grid(row=6,column=3)
  97.  
  98. window.mainloop()
  99.  
Runtime error #stdin #stdout #stderr 0.04s 26096KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 50, in <module>
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1854, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable