Example:2
Python script to take input for two numbers, further calculate and print sum of numbers.
Sol:
Output:
#textbox/Entry from tkinter import * window = Tk() window.title("Sum of two numbers ") window.geometry('350x200') #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 clicked1(): #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=140) def clicked2(): 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=140) btn_1 = Button(window, text="Calculate", command=clicked1) btn_1.place(x=20, y=100) btn_2 = Button(window, text="Clear", command=clicked2) btn_2.place(x=120, y=100) btn_3 = Button(window, text="Exit", command=window.destroy) btn_3.pack() btn_3.place(x=220, y=100) window.mainloop()
Example:3
Python script to take input for two numbers, further calculate and print their sum, product and difference.
Sol:
Output:
#textbox/Entry from tkinter import * window = Tk() window.title("Sum 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 clicked1(): #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=140) c=a*b lbl_3=Label(window,text=" Product = "+str(c)) lbl_3.place(x=20,y=180) if(a>b): c=a-b else: c=b-a lbl_3=Label(window,text=" Difference = "+str(c)) lbl_3.place(x=20,y=220) def clicked2(): 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=140) lbl_3=Label(window,text=" ") lbl_3.place(x=20,y=180) lbl_3=Label(window,text=" ") lbl_3.place(x=20,y=220) btn_1 = Button(window, text="Calculate", command=clicked1) btn_1.place(x=20, y=100) btn_2 = Button(window, text="Clear", command=clicked2) btn_2.place(x=120, y=100) btn_3 = Button(window, text="Exit", command=window.destroy) btn_3.pack() btn_3.place(x=220, y=100) window.mainloop()