Monday, December 4, 2017

Tip class with tkinter form

First I have created the tip class. It should be saved to the same directory as the form. Also to import it the __init__.py file must be in the directory. (just the name it doesn't need any content.). Here is the Tip class:

class Tip:
    def __init__(self, amount, tipPercent, taxPercent):
        self.amount=amount
        self.tipPercent=tipPercent
        self.taxPercent=taxPercent

    def calculateTax(self):
        if self.taxPercent >= 1:
            self.taxPercent=self.taxPercent / 100.0
        self.tax=self.amount * self.taxPercent

    def calculateTip(self):
        if self.tipPercent >=1:
            self.tipPercent=self.tipPercent / 100.0
        self.tip=self.amount * self.tipPercent

    def calculateTotal(self):
        self.total=self.amount + self.tip + self.tax
    

    def getTax(self):
        return self.tax

    def getTip(self):
        return self.tip

    def getTotal(self):
        return self.total

    def __str__(self):
        self.calculateTax()
        self.calculateTip()
        self.calculateTotal()
        return ("Amount: " + str(self.amount) + 
               " Tax: " + str(self.tax) + " Tip: " + str(self.tip) + 
               " Total: " + str(self.total))



    
        

Now here is the form class. It calls tkinter and the Tip class. I used the message box for the ouput, because it is surprisingly difficult to write to the labels.

from tkinter import *
from Tip import Tip
from tkinter import messagebox

class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master=master
        self.init_window()

    def init_window(self):
        self.master.title("Tip Calculator")
        
        self.amountLabel=Label(self.master, text="Enter Amount")
        self.amountLabel.place(x=10, y=15)

        self.amountEntry=Entry(self.master)
        self.amountEntry.place(x=10, y=35)

        self.taxLabel=Label(self.master, text="Enter tax Percent (no % sign)")
        self.taxLabel.place(x=10,y=55)
        
        self.taxEntry=Entry(self.master)
        self.taxEntry.place(x=10, y=75)

        self.tipLabel=Label(self.master, text="Enter tip Percent (no % sign)")
        self.tipLabel.place(x=10,y=105)
        
        self.tipEntry=Entry(self.master)
        self.tipEntry.place(x=10, y=125)

        self.submit=Button(self.master, text="Submit", command=self.onClick)
        self.submit.place(x=10,y=150)

    def onClick(self):
        amount=float(self.amountEntry.get())
        taxPerc=float(self.taxEntry.get())
        tipPerc=float(self.tipEntry.get())
        t=Tip(amount, tipPerc, taxPerc)
        #message = print(t)
        messagebox.showinfo("total due", t)
            

        
        


root=Tk()
root.geometry=("800x800")

app=Window(root)
root.mainloop()

There are many many improvements and refinements that could be made on this form, but it provides a basic template. I used absolute positioning, but grid positioning is more flexible. You should look it up and try to implement it.

No comments:

Post a Comment