I am a newbie on the programming world and I am trying to learn Python as it is the easiest and very strong programming language. I've learnt all basic theories and the programming syntax and then I have solved some problems from urionlinejudge. Now I'am trying to learn GUI Appliations and I am trying to learn Tkinter module of Python.
Previously I posted a Restaurant Management System GUI application that I made with Python and Tkinter module of python. Here is Phonebook GUI Application Made With Python and Tkinter. I will post the code into different parts. This is the second part.
First Part
Let us jump to the codes of main.py file.
Importing
import tkinter as tk
from tkinter import PhotoImage
import datetime
from my_people import my_people
from add_people import Add_people
from aboutus import About
date = datetime.datetime.now().date()
date = str(date)
Class and Functions
class Application(object):
def __init__(self, master):
self.master = master
# frames
self.top = tk.Frame(master, height=150, bg='white')
self.top.pack(fill=tk.X)
self.bottom = tk.Frame(master, height=500, bg='#34baeb')
self.bottom.pack(fill=tk.X)
# ---------- icon ------------
self.top_image = PhotoImage(file='icons/phonebook.png')
self.top_image_label = tk.Label(self.top, image=self.top_image, bg='white')
self.top_image_label.place(x=130, y=25)
# -------------------- heading ----------------
self.heading = tk.Label(self.top, text='My Phone Book', bg='white', font='arial 15 bold', fg='#ebb434')
self.heading.place(x=230, y=40)
# ---------------------- date ----------------------
self.date_lbl = tk.Label(self.top, text=f'Date: {date}', font='arial 11 bold', bg='white', fg='#ebb434')
self.date_lbl.place(x=500, y=10)
# ------------- view button -----------------
self.view_btn = tk.Button(self.bottom, text='My People', font='arial 14 bold', width=14, bg='white', fg='#ebb434', command=self.My_people)
self.view_btn.place(x=230, y=40)
# ------------------- add btn --------------------
self.add_btn = tk.Button(self.bottom, text='Add People', font='arial 14 bold', width=14, bg='white', fg='#ebb434', command=self.add_people)
self.add_btn.place(x=230, y=100)
# ------------------- about us ------------------
self.about_btn = tk.Button(self.bottom, text='About Us', font='arial 14 bold', width=14, bg='white', fg='#ebb434', command = About)
self.about_btn.place(x=230, y=160)
def My_people(self):
people = my_people()
def add_people(self):
addpeople = Add_people()
def main():
window = tk.Tk()
app = Application(window)
window.geometry('650x550+380+80')
window.resizable(0, 0)
window.title('Phone Book')
window.mainloop()
if __name__ == '__main__':
main()
The full main.py file's code is:-
import tkinter as tk
from tkinter import PhotoImage
import datetime
from my_people import my_people
from add_people import Add_people
from aboutus import About
date = datetime.datetime.now().date()
date = str(date)
class Application(object):
def __init__(self, master):
self.master = master
# frames
self.top = tk.Frame(master, height=150, bg='white')
self.top.pack(fill=tk.X)
self.bottom = tk.Frame(master, height=500, bg='#34baeb')
self.bottom.pack(fill=tk.X)
# ---------- icon ------------
self.top_image = PhotoImage(file='icons/phonebook.png')
self.top_image_label = tk.Label(self.top, image=self.top_image, bg='white')
self.top_image_label.place(x=130, y=25)
# -------------------- heading ----------------
self.heading = tk.Label(self.top, text='My Phone Book', bg='white', font='arial 15 bold', fg='#ebb434')
self.heading.place(x=230, y=40)
# ---------------------- date ----------------------
self.date_lbl = tk.Label(self.top, text=f'Date: {date}', font='arial 11 bold', bg='white', fg='#ebb434')
self.date_lbl.place(x=500, y=10)
# ------------- view button -----------------
self.view_btn = tk.Button(self.bottom, text='My People', font='arial 14 bold', width=14, bg='white', fg='#ebb434', command=self.My_people)
self.view_btn.place(x=230, y=40)
# ------------------- add btn --------------------
self.add_btn = tk.Button(self.bottom, text='Add People', font='arial 14 bold', width=14, bg='white', fg='#ebb434', command=self.add_people)
self.add_btn.place(x=230, y=100)
# ------------------- about us ------------------
self.about_btn = tk.Button(self.bottom, text='About Us', font='arial 14 bold', width=14, bg='white', fg='#ebb434', command = About)
self.about_btn.place(x=230, y=160)
def My_people(self):
people = my_people()
def add_people(self):
addpeople = Add_people()
def main():
window = tk.Tk()
app = Application(window)
window.geometry('650x550+380+80')
window.resizable(0, 0)
window.title('Phone Book')
window.mainloop()
if __name__ == '__main__':
main()
Let's jump to the codes of aboutus.py
import tkinter as tk
from tkinter import PhotoImage
import datetime
date = datetime.datetime.now().date()
date = str(date)
class About(tk.Toplevel):
def __init__(self):
tk.Toplevel.__init__(self)
self.geometry('650x550+380+80')
self.title('My People')
self.resizable(0,0)
self.top = tk.Frame(self, height=150, bg='white')
self.top.pack(fill=tk.X)
self.bottom = tk.Frame(self, height=500, bg='black')
self.bottom.pack(fill=tk.BOTH, expand=True)
# ---------- icon ------------
self.top_image = PhotoImage(file='icons/people.png')
self.top_image_label = tk.Label(self.top, image=self.top_image, bg='white')
self.top_image_label.place(x=130, y=25)
# -------------------- heading ----------------
self.heading = tk.Label(self.top, text='About Us', bg='white', font='arial 15 bold', fg='#34baeb')
self.heading.place(x=230, y=40)
# ------------------------ date -------------------------------
self.date_lbl = tk.Label(self.top, text=f'Date: {date}', font='arial 11 bold', bg='white', fg='#ebb434')
self.date_lbl.place(x=500, y=10)
# ------------------ label --------------------
about_us_text = 'Hi! This is made for educational purpose.\n If you want to contact us,\n send us email on "[email protected]"\n'
self.text_label = tk.Label(self.bottom, text=about_us_text, bg='black', fg='white', font='arial 14 bold')
self.text_label.pack(pady=120)