Thursday, February 23, 2017

Simple Add/Sub/Div/Multiplication application in Python with Tk GUI

image_1image_2image_3image_4

from tkinter import *
import tkinter
from tkinter import messagebox
from lib2to3.fixer_util import Number

top = tkinter.Tk()
L1 = Label(top, text="First Number")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)
E1.pack(side = LEFT)

L2 = Label(top, text="Second Number")
L2.pack( side = LEFT)
E2 = Entry(top, bd =5)
E2.pack(side = LEFT)
L3 = Label(top, text="Answer")
L3.pack( side = LEFT)
E3 = Entry(top, bd =5)
E3.pack(side = LEFT)
def helloCallBack(selection):
print("E1.get()"+E1.get())
print("E2.get()"+E2.get())
print("selection"+selection)
a = int(E1.get())
b = int(E2.get())
if selection in ('Addition'):
answer = a + b
print("answer"+str(answer))
E3.delete(0,'end')
E3.insert(0, answer)
elif selection in ('Substraction'): 
answer = a - b
print("answer"+str(answer))
E3.delete(0,'end')
E3.insert(0, answer)
elif selection in ('Division'): 
answer = a / b
print("answer"+str(answer))
E3.delete(0,'end')
E3.insert(0, answer) 
else: 
answer = a * b
print("answer"+str(answer))
E3.delete(0,'end')
E3.insert(0, answer) 
def sel():
selection = "You selected the option " + str(var.get())
label.config(text = selection) 
if var.get() == 1:
#B.destroy()
selection = 'Addition'
label.config(text = selection) 

elif var.get() == 2:
#B.destroy()
selection = 'Substraction'
label.config(text = selection)

elif var.get() == 3:
#B.destroy()
selection = 'Division'
label.config(text = selection)


else:
#B.destroy()
selection = 'Multiplication'
label.config(text = selection)
B = tkinter.Button(text =selection, command=lambda: helloCallBack(selection))
B.pack(side = LEFT) 

var = IntVar()
R1 = Radiobutton(top, text="Add", variable=var, value=1, command=sel)
R1.pack( anchor = W )
R2 = Radiobutton(top, text="Subtract", variable=var, value=2, command=sel)
R2.pack( anchor = W )
R3 = Radiobutton(top, text="Division", variable=var, value=3, command=sel)
R3.pack( anchor = W)
R4 = Radiobutton(top, text="Multiplications", variable=var, value=4, command=sel)
R4.pack( anchor = W)
label = Label(top)
label.pack()
top.mainloop()

No comments: