Creating GUI Applications using tkinter

Download the package tk

conda install tk

pip install tk


# create a new frame
from tkinter import *
root = Tk()
root.geometry("600x250")
root.mainloop()


# Using Label in Frame
from tkinter import *
root= Tk()
root.geometry("600x250")
my_text= Label(root, text= "This is a New Line Text", font=('Helvetica bold', 16))
my_text.pack(pady=30)
root.mainloop()


# Adding Buttons
from tkinter import *
from tkinter import ttk
win= Tk()
win.geometry("400x300")

def save():
   print("Saved")

cmdsave= ttk.Button(win, text="Save",command=lambda:save())
cmdsave.pack()
win.mainloop()


# Using Images
from tkinter import *
root= Tk()
root.geometry("750x280")
canvas= Canvas(root, width=300, height=300)
logo = PhotoImage(file='logo.png')
canvas.create_image((200, 140), image=logo)
canvas.pack()
root.mainloop()


# Sample GUI Application
from tkinter import *
fields = ('Annual Rate', 'Number of Payments', 'Loan Principle', 'Monthly Payment')
def monthly_payment(entries):
   r = (float(entries['Annual Rate'].get()) / 100) / 12
   loan = float(entries['Loan Principle'].get())
   n = float(entries['Number of Payments'].get())
   q = (1 + r)** n
   monthly = r * ( (q * loan) / ( q - 1 ))
   monthly = ("%8.2f" % monthly).strip()
   entries['Monthly Payment'].delete(0,END)
   entries['Monthly Payment'].insert(0, monthly )
def makeform(root, fields):
   entries = {}
   for field in fields:
      row = Frame(root)
      lab = Label(row, width=22, text=field+": ", anchor='w')
      ent = Entry(row)
      ent.insert(0,"0")
      row.pack(side = TOP, fill = X, padx = 5 , pady = 5)
      lab.pack(side = LEFT)
      ent.pack(side = RIGHT, expand = YES, fill = X)
      entries[field] = ent
   return entries
if __name__ == '__main__':
   root = Tk()
   ents = makeform(root, fields)
   b1 = Button(root, text='Monthly Payment',command=lambda: monthly_payment(ents))
   b1.pack(side = LEFT, padx = 5, pady = 5)
   root.mainloop()

Comments

Popular posts from this blog

Converting Excel File to JSON File using xlrd module

Using Oracle with Python

Using time module