![[Image: DiscountApp.jpg]](https://4.bp.blogspot.com/-qyeHRhWrD9s/XHgasFRNZ8I/AAAAAAAACMs/RRM9m11aLz4aIie4Om9MiRPmOxm1KN9swCLcBGAs/s1600/DiscountApp.jpg)
Hello friends, I have created this simple GUI using TkInter. Am I doing it right?
Here is the link to my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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() |