Friday, March 15, 2019

Discount App

Here is a simple Discount App made with tkinter. This program will run only in Python 3. To run this program in Python 2, you will need to modify it accordingly.

Code:

import tkinter as tk
from tkinter import messagebox
class myApp:
def __init__(self):
self.root = tk.Tk()
self.e1 = tk.StringVar()
self.e2 = tk.StringVar()
self.e3 = tk.StringVar()
label1 = tk.Label(self.root, text='Price:')
label2 = tk.Label(self.root, text='Discount:')
label3 = tk.Label(self.root, text='%')
label4 = tk.Label(self.root, text='Amount:')
entry1 = tk.Entry(self.root, textvariable=self.e1)
entry2 = tk.Entry(self.root, textvariable=self.e2)
entry3 = tk.Entry(self.root, textvariable=self.e3, state='readonly')
btn1 = tk.Button(self.root, text='Calculate',command=button_click)
label1.grid(row=0,column=0,padx=(10,5),pady=(10,5),sticky=tk.W)
entry1.grid(row=0,column=1,padx=(5,5),pady=(10,5))
label2.grid(row=1,column=0,padx=(10,5),pady=(5,5),sticky=tk.W)
entry2.grid(row=1,column=1,padx=(5,5),pady=(5,5))
label3.grid(row=1,column=2,padx=(5,10),pady=(5,5))
btn1.grid(row=2,column=1,padx=(5,5),pady=(5,5),sticky=tk.W)
label4.grid(row=3,column=0,padx=(10,5),pady=(5,10),sticky=tk.W)
entry3.grid(row=3,column=1,padx=(5,5),pady=(5,10),sticky=tk.W)
self.root.resizable(False,False)
self.root.title('Discount App')
def button_click():
try:
price = float(app.e1.get())
discount = float(app.e2.get())
except:
messagebox.showwarning('Invalid Input','Please enter numbers only.')
else:
amount = price*(1-discount/100)
app.e3.set(str(f'{amount:0.2f}'))
app = myApp()
app.root.mainloop()
view raw DiscountApp.py hosted with ❤ by GitHub
Output:

To know more about TkInter, please visit tkinter documentation page here.

2 comments:

Unknown said...

Excellent buddy, thanks for sharing. Just one friendly tip to improve the post: No comments on the source :)

Namasudra said...

Thanks buddy for the tip. I will try to edit. :)