Example:4
Python script to take input for two numbers, further calculate and print their sum, product and difference using different buttons.
Sol:
Output:
#textbox/Entry from tkinter import * window = Tk() window.title("Sum, Product and Difference of two numbers ") window.geometry('350x300') #1st label and textbox lbl_1 = Label(window, text="Enter 1st no ") lbl_1.place(x=20, y=40) txt_1 = Entry(window,width=10) txt_1.place(x=120, y=40) #2nd label and textbox lbl_2 = Label(window, text="Enter 2nd no ") lbl_2.place(x=20, y=70) txt_2 = Entry(window,width=10) txt_2.place(x=120, y=70) #label to display sum lbl_3=Label(window,text="") lbl_3.place(x=20,y=140) def sum(): #res = "Welcome " + txt.get() a=int(txt_1.get()) b=int(txt_2.get()) c=a+b lbl_3=Label(window,text=" Sum = "+str(c)) lbl_3.place(x=20,y=200) def product(): #res = "Welcome " + txt.get() a=int(txt_1.get()) b=int(txt_2.get()) c=a*b lbl_3=Label(window,text=" Product = "+str(c)) lbl_3.place(x=20,y=200) def difference(): #res = "Welcome " + txt.get() a=int(txt_1.get()) b=int(txt_2.get()) if(a>b): c=a-b else: c=b-a lbl_3=Label(window,text=" Difference = "+str(c)) lbl_3.place(x=20,y=200) def clear(): res = " " txt_1=Entry(window,width=10,text=res) txt_1.place(x=120, y=40) txt_2=Entry(window,width=10,text=res) txt_2.place(x=120, y=70) lbl_3=Label(window,text=" ") lbl_3.place(x=20,y=200) btn_1 = Button(window, text="Sum", command=sum) btn_1.place(x=10, y=100) btn_1 = Button(window, text="Product", command=product) btn_1.place(x=60, y=100) btn_1 = Button(window, text="Difference", command=difference) btn_1.place(x=120, y=100) btn_2 = Button(window, text="Clear", command=clear) btn_2.place(x=40, y=140) btn_3 = Button(window, text="Exit", command=window.destroy) btn_3.pack() btn_3.place(x=100, y=140) window.mainloop()