fork download
  1. import re
  2. from tkinter import *
  3. class Calc():
  4. s = ''
  5. def add_it(n):
  6. screen.insert(END, n)
  7. Calc.s+=n
  8. def equ():
  9. x = float(re.findall(r'\-*\d+\.*\d*',Calc.s)[0])
  10. y = float(re.findall(r'\d+\.*\d*',Calc.s)[1])
  11. act = re.search(r'[+*/-]{1}', Calc.s[1:]).group()
  12. if act == '+':
  13. z = x+y
  14. elif act == '-':
  15. z = x-y
  16. elif act == '*':
  17. z = x*y
  18. else:
  19. z = x/y
  20. screen.delete(0,END)
  21. screen.insert(END,z)
  22. Calc.s = str(z)
  23. def clear():
  24. screen.delete(0,END)
  25. Calc.s=''
  26.  
  27. window = Tk()
  28. window.resizable(0,0)
  29. window.geometry('228x220')
  30. window.title('Simple Calculator')
  31. screen = Entry(window, bd=8, font = 3)
  32. one = Button(window, text='1',height=2,width=6,bd=4,bg='azure', command=lambda event='<Button-1>' : Calc.add_it('1'))
  33. two = Button(window, text='2',height=2,width=6,bd=4,bg='azure',command=lambda event='<Button-1>' : Calc.add_it('2'))
  34. three = Button(window, text='3',height=2,width=6,bd=4,bg='azure', command=lambda event='<Button-1>' : Calc.add_it('3'))
  35. four = Button(window, text='4',height=2,width=6,bd=4, bg='azure',command=lambda event='<Button-1>' : Calc.add_it('4'))
  36. five = Button(window, text='5',height=2,width=6, bd=4,bg='azure',command=lambda event='<Button-1>' : Calc.add_it('5'))
  37. six = Button(window, text='6',height=2,width=6, bd=4,bg='azure',command=lambda event='<Button-1>' : Calc.add_it('6'))
  38. seven = Button(window, text='7',height=2,width=6,bd=4,bg='azure', command=lambda event='<Button-1>' : Calc.add_it('7'))
  39. eight = Button(window, text='8',height=2,width=6,bd=4,bg='azure', command=lambda event='<Button-1>' : Calc.add_it('8'))
  40. nine = Button(window, text='9',height=2,width=6, bd=4,bg='azure',command=lambda event='<Button-1>' : Calc.add_it('9'))
  41. zero = Button(window, text='0',height=2,width=6,bd=4, bg='azure',command=lambda event='<Button-1>' : Calc.add_it('0'))
  42. plus = Button(window, text='+',height=2,width=6,bd=4,bg='azure', command=lambda event='<Button-1>' : Calc.add_it('+'))
  43. minus = Button(window, text='-',height=2,width=6,bd=4,bg='azure', command=lambda event='<Button-1>' : Calc.add_it('-'))
  44. div = Button(window, text='/',height=2,width=6,bd=4, bg='azure',command=lambda event='<Button-1>' : Calc.add_it('/'))
  45. mult = Button(window, text='*',height=2,width=6, bd=4,bg='azure',command=lambda event='<Button-1>' : Calc.add_it('*'))
  46. equ = Button(window, text='=',height=2,width=6,bd=4, bg='azure',command=Calc.equ)
  47. clear = Button(window, text='C',height=2,width=6,bd=4, bg='azure',fg='red',command=Calc.clear)
  48.  
  49. screen.grid(row=0,rowspan=2,columnspan=12)
  50. one.grid(row=2,column=0)
  51. two.grid(row=2,column=1)
  52. three.grid(row=2,column=2)
  53. four.grid(row=2,column=3)
  54. five.grid(row=3,column=0)
  55. six.grid(row=3,column=1)
  56. seven.grid(row=3,column=2)
  57. eight.grid(row=3,column=3)
  58. nine.grid(row=4,column=0)
  59. zero.grid(row=4,column=1)
  60. plus.grid(row=4,column=2)
  61. minus.grid(row=4,column=3)
  62. mult.grid(row=5,column=0)
  63. div.grid(row=5,column=1)
  64. clear.grid(row=5,column=2)
  65. equ.grid(row=5,column=3)
  66.  
  67. window.mainloop()
Runtime error #stdin #stdout #stderr 0.04s 26088KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 27, 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