Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # Tk_basic_calc_demo.py
- from Tkinter import *
- import tkFont
- #ACTIONS
- def clear():
- #Action: Clears the entry()
- txtDisplay.delete(0,END)
- return
- ERROR='< ERROR >'
- def update_Entry(inputValue):
- #Updates the entry when a button is pressed
- currentText=num1.get()
- currentText=currentText.replace(ERROR,'')
- update=num1.set(currentText + inputValue)
- def evaluate():
- currentText=num1.get()
- currentText=currentText+'*1.0'
- try:
- num1.set(str(eval(currentText)))
- except:
- num1.set(ERROR)
- #Parent Window
- root=Tk()
- root.title('Basic Calculator')
- # root.geometry('100x100')
- #Main entry
- num1=StringVar()
- displayFont = ('Verdana',21)
- txtDisplay=Entry(root, textvariable=num1, relief=RIDGE, bd=3, insertwidth=1, font=displayFont, justify=RIGHT)
- txtDisplay.place(x=12, y=16)
- txtDisplay.place(width=310, height=40)
- txtDisplay.focus()
- print(txtDisplay.get())
- #Pseudo y-axis padding
- ppad=10
- y1=65;y2=105+(ppad);y3=145+(ppad*2);y4=185+(ppad*3);y5=225+(ppad*4)
- default_font=tkFont.nametofont('TkDefaultFont')
- default_font.configure(family='Tahoma', size=16, weight='bold')
- #Buttons : Note '/' equals '÷'
- zeroButton=Button(root, text='0', width=11, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('0'))
- zeroButton.place(x=12,y=y5)
- oneButton=Button(root, text='1', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('1'))
- oneButton.place(x=12, y=y4)
- twoButton=Button(root, text='2', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('2'))
- twoButton.place(x=90, y=y4)
- threeButton=Button(root, text='3', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('3'))
- threeButton.place(x=168, y=y4)
- fourButton=Button(root, text='4', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('4'))
- fourButton.place(x=12, y=y3)
- fiveButton=Button(root, text='5', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('5'))
- fiveButton.place(x=90, y=y3)
- sixButton=Button(root, text='6', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('6'))
- sixButton.place(x=168, y=y3)
- sevenButton=Button(root, text='7', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('7'))
- sevenButton.place(x=12, y=y2)
- eightButton=Button(root, text='8', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('8'))
- eightButton.place(x=90, y=y2)
- ninthButton=Button(root, text='9', width=5, height=0, bg='LightBlue', fg='red', command=lambda:update_Entry('9'))
- ninthButton.place(x=168, y=y2)
- decimalButton=Button(root, text='.', width=5, height=0, bg='powder blue', command=lambda:update_Entry('.'))
- decimalButton.place(x=168, y=y5)
- plusButton=Button(root, text='+', width=5, height=0, bg='gray', command=lambda:update_Entry('+'))
- plusButton.place(x=246, y=y3)
- minusButton=Button(root, text='-', width=5, height=0, bg='gray', command=lambda:update_Entry('-'))
- minusButton.place(x=246, y=y2)
- multiplyButton=Button(root, text='x', width=5, height=0, bg='gray', command=lambda:update_Entry('*'))
- multiplyButton.place(x=246, y=y1)
- divideButton=Button(root, text='÷', width=5, height=0, bg='gray', command=lambda:update_Entry('/'))
- divideButton.place(x=168, y=y1)
- clearButton=Button(root, text='Clear (CE)', width=11, height=0, command=clear, bg='Orange')
- clearButton.place(x=12, y=y1)
- equalButton=Button(root, text='=', width=5, height=3, bg='Lightgreen', command=lambda:evaluate())
- equalButton.place(x=246, y=y4)
- #Locks the parent windows size
- root.maxsize(334,340)
- root.minsize(334,340)
- #Parent window's background color
- root.configure(background='blue')
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement