Posted By

nicodasiko on 07/10/14


Tagged

Calculator gui


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Jay-Jay


My first GUI python calculator


 / Published in: Python
 

URL: https://drive.google.com/folderview?id=0B1PcXPKwwMd-RGt1N3hMVnItODA&usp=drive_web

My first calculator with GUI. I made the GUI with tkinter.

  1. from Tkinter import *
  2. import tkMessageBox
  3.  
  4. def calc():
  5. try:
  6. #addition
  7. if oper.get() == '+':
  8. try:
  9. value = float(num1.get()) + float(num2.get())
  10. except:
  11. tkMessageBox.showerror("Error!", 'Something went wrong! Maybe invalid entries')
  12.  
  13. #subtraction
  14. if oper.get() == '-':
  15. try:
  16. value = float(num1.get()) - float(num2.get())
  17. except:
  18. tkMessageBox.showerror("Error!", 'Something went wrong! Maybe invalid entries')
  19. #multiplication
  20. if oper.get() == 'x' or oper.get() == '*':
  21. try:
  22. value = float(num1.get()) * float(num2.get())
  23. except:
  24. tkMessageBox.showerror("Error!", 'Something went wrong! Maybe invalid entries')
  25. #Division
  26. if oper.get() == '/':
  27. if num2.get() == '0':
  28. #checks if user is trying to divide by zero
  29. tkMessageBox.showerror('Division Error', 'num2 value = 0 No dividing by 0')
  30. else:
  31. try:
  32. value = float(num1.get()) / float(num2.get())
  33. except:
  34. tkMessageBox.showerror("Error!", 'Something went wrong! Maybe invalid entries')
  35.  
  36. str(num1)
  37. str(oper)
  38. str(num2)
  39. answer = Label(app, text='{0} {1} {2}='.format(num1.get(), oper.get(), num2.get()))
  40. answer.grid(row=3, column= 0, sticky=SW)
  41. answer2 = Label(app, text=value)
  42. answer2.grid(row=4, column= 0, sticky=S)
  43. except:
  44. tkMessageBox.showerror("Error!", 'Something went wrong! Maybe invalid entries')
  45.  
  46.  
  47. global value
  48. value = 0
  49.  
  50. #Modify window
  51. root = Tk()
  52. root.title('Calculator by Nicholas Cannon')
  53. root.geometry('340x132')
  54. root.resizable(0,0)
  55.  
  56.  
  57. #creating frame
  58. app = Frame(root)
  59. app.pack()
  60.  
  61. #window attributes below
  62. label1 = Label(app, text='Enter number 1 -->')
  63. label1.grid(row=0, column=0, sticky=NW)
  64.  
  65. label2 = Label(app, text='Enter operator -->')
  66. label2.grid(row=1, column=0, sticky=W)
  67.  
  68. label3 = Label(app, text='Enter number 2 -->')
  69. label3.grid(row=2, column= 0, sticky=W)
  70.  
  71. global num1
  72. num1 = StringVar()
  73. number1 = Entry(app, textvariable= num1)
  74. number1.grid(row=0, column=1, sticky=NE)
  75.  
  76. global oper
  77. oper = StringVar()
  78. operator = Entry(app, textvariable= oper)
  79. operator.grid(row=1, column=1, sticky=E)
  80.  
  81. global num2
  82. num2 = StringVar()
  83. number2 = Entry(app, textvariable = num2)
  84. number2.grid(row=2, column=1, sticky=E)
  85.  
  86. button = Button(app, text='Calculate', command = calc)
  87. button.grid(row=3, column=1, sticky=SE)
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. #start main loop
  96. root.mainloop()

Report this snippet  

You need to login to post a comment.