Advertisement
here2share

# Tk_basic_calc_demo.py

Jul 19th, 2015
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1.  # -*- coding: utf-8 -*-
  2. # Tk_basic_calc_demo.py
  3.  
  4. from Tkinter import *
  5. import tkFont
  6.  
  7. #ACTIONS
  8. def clear():
  9.     #Action: Clears the entry()
  10.     txtDisplay.delete(0,END)
  11.     return
  12.  
  13. ERROR='< ERROR >'
  14. def update_Entry(inputValue):
  15.     #Updates the entry when a button is pressed
  16.     currentText=num1.get()
  17.     currentText=currentText.replace(ERROR,'')
  18.     update=num1.set(currentText + inputValue)
  19.  
  20. def evaluate():
  21.     currentText=num1.get()
  22.     currentText=currentText+'*1.0'
  23.     try:
  24.         num1.set(str(eval(currentText)))
  25.     except:
  26.         num1.set(ERROR)
  27.  
  28. #Parent Window
  29. root=Tk()
  30. root.title('Basic Calculator')
  31. # root.geometry('100x100')
  32.  
  33. #Main entry
  34. num1=StringVar()
  35. displayFont = ('Verdana',21)
  36. txtDisplay=Entry(root, textvariable=num1, relief=RIDGE, bd=3, insertwidth=1, font=displayFont, justify=RIGHT)
  37. txtDisplay.place(x=12, y=16)
  38. txtDisplay.place(width=310, height=40)
  39. txtDisplay.focus()
  40.  
  41. print(txtDisplay.get())
  42.  
  43. #Pseudo y-axis padding
  44. ppad=10
  45. y1=65;y2=105+(ppad);y3=145+(ppad*2);y4=185+(ppad*3);y5=225+(ppad*4)
  46.  
  47. default_font=tkFont.nametofont('TkDefaultFont')
  48. default_font.configure(family='Tahoma', size=16, weight='bold')
  49.  
  50. #Buttons : Note '/' equals '÷'
  51. zeroButton=Button(root, text='0', width=11, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('0'))
  52. zeroButton.place(x=12,y=y5)
  53. oneButton=Button(root, text='1', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('1'))
  54. oneButton.place(x=12, y=y4)
  55. twoButton=Button(root, text='2', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('2'))
  56. twoButton.place(x=90, y=y4)
  57. threeButton=Button(root, text='3', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('3'))
  58. threeButton.place(x=168, y=y4)
  59. fourButton=Button(root, text='4', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('4'))
  60. fourButton.place(x=12, y=y3)
  61. fiveButton=Button(root, text='5', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('5'))
  62. fiveButton.place(x=90, y=y3)
  63. sixButton=Button(root, text='6', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('6'))
  64. sixButton.place(x=168, y=y3)
  65. sevenButton=Button(root, text='7', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('7'))
  66. sevenButton.place(x=12, y=y2)
  67. eightButton=Button(root, text='8', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('8'))
  68. eightButton.place(x=90, y=y2)
  69. ninthButton=Button(root, text='9', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('9'))
  70. ninthButton.place(x=168, y=y2)
  71.  
  72. decimalButton=Button(root, text='.', width=5, height=0, bg='powder blue', command=lambda:update_Entry('.'))
  73. decimalButton.place(x=168, y=y5)
  74. plusButton=Button(root, text='+', width=5, height=0, bg='gray', command=lambda:update_Entry('+'))
  75. plusButton.place(x=246, y=y3)
  76. minusButton=Button(root, text='-', width=5, height=0, bg='gray', command=lambda:update_Entry('-'))
  77. minusButton.place(x=246, y=y2)
  78. multiplyButton=Button(root, text='x', width=5, height=0, bg='gray', command=lambda:update_Entry('*'))
  79. multiplyButton.place(x=246, y=y1)
  80. divideButton=Button(root, text='÷', width=5, height=0, bg='gray', command=lambda:update_Entry('/'))
  81. divideButton.place(x=168, y=y1)
  82. clearButton=Button(root, text='Clear (CE)', width=11, height=0, command=clear, bg='Orange')
  83. clearButton.place(x=12, y=y1)
  84. equalButton=Button(root, text='=', width=5, height=3, bg='Lightgreen', command=lambda:evaluate())
  85. equalButton.place(x=246, y=y4)
  86.  
  87. #Locks the parent windows size
  88. root.maxsize(334,340)
  89. root.minsize(334,340)
  90.  
  91. #Parent window's background color
  92. root.configure(background='blue')
  93. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement