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 third part.
First Part
Second Part
Third Part
Let us jump to the codes of "update_people.py".
Importing
import tkinter as tk
import _sqlite3
from tkinter import PhotoImage
import datetime
from tkinter import messagebox
date = datetime.datetime.now().date()
date = str(date)
con = _sqlite3.connect('database.db')
cur = con.cursor()
Class "Update_people"
class Update_people(tk.Toplevel):
def __init__(self, person_id):
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='#ebb134')
self.bottom.pack(fill=tk.X)
# ---------- 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='Update People', 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)
# ------------- query -----------------------
query = 'select * from "addressbook" where person_id = {}'.format(person_id)
result = cur.execute(query).fetchone()
self.person_id = person_id
person_name = result[1]
person_surname = result[2]
email = result[3]
phone = result[4]
address = result[5]
#------------------ name ---------------
self.label_name = tk.Label(self.bottom, text='Name: ', font='Arial 12 bold', bg='#ebb134')
self.label_name.place(x=40, y=40)
self.entry_name = tk.Entry(self.bottom, width=30, bd=4)
self.entry_name.insert(0, person_name)
self.entry_name.place(x=175, y=40)
#------------- surname -------------------
self.label_surname = tk.Label(self.bottom, text='Surname: ', font='Arial 12 bold', bg='#ebb134')
self.label_surname.place(x=40, y=80)
self.entry_surname = tk.Entry(self.bottom, width=30, bd=4)
self.entry_surname.insert(0, person_surname)
self.entry_surname.place(x=175, y=80)
#------------------ email --------------------
self.label_email = tk.Label(self.bottom, text='Email: ', font='Arial 12 bold', bg='#ebb134')
self.label_email.place(x=40, y=120)
self.entry_email = tk.Entry(self.bottom, width=30, bd=4)
self.entry_email.insert(0, email)
self.entry_email.place(x=175, y=120)
#------------- phone number ---------------
self.label_phone = tk.Label(self.bottom, text='Phone Number: ', font='Arial 12 bold', bg='#ebb134')
self.label_phone.place(x=40, y=160)
self.entry_phone = tk.Entry(self.bottom, width=30, bd=4)
self.entry_phone.insert(0, phone)
self.entry_phone.place(x=175, y=160)
#------------ address -------------
self.label_Address = tk.Label(self.bottom, text='Address: ', font='Arial 12 bold', bg='#ebb134')
self.label_Address.place(x=40, y=200)
self.entry_Address = tk.Text(self.bottom, width=24, height=3, wrap='word')
self.entry_Address.insert(1.0, address)
self.entry_Address.place(x=175, y=200)
#--------- submit btn ----------------
self.submit_btn = tk.Button(self.bottom, text='Update', font='Arial 12 bold', command=self.update_people)
self.submit_btn.place(x=130, y=280)
Functions inside "Update_people" class
def update_people(self):
person_id = self.person_id
name = self.entry_name.get()
surname = self.entry_surname.get()
email = self.entry_email.get()
phone = self.entry_phone.get()
address = self.entry_Address.get(1.0, 'end-1c')
query = "update 'addressbook' set person_name = '{}', person_surname = '{}', person_email = '{}', person_phone = '{}', person_address = '{}' where person_id = {}".format(name, surname, email, phone, address, person_id)
try:
cur.execute(query)
con.commit()
tk.messagebox.showinfo('Success', 'Information Successfully Updated')
self.destroy()
except Exception as e:
tk.messagebox.showerror('Error', str(e))
self.destroy()
So the full codes of "update_people.py" is :-
import tkinter as tk
import _sqlite3
from tkinter import PhotoImage
import datetime
from tkinter import messagebox
date = datetime.datetime.now().date()
date = str(date)
con = _sqlite3.connect('database.db')
cur = con.cursor()
class Update_people(tk.Toplevel):
def __init__(self, person_id):
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='#ebb134')
self.bottom.pack(fill=tk.X)
# ---------- 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='Update People', 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)
# ------------- query -----------------------
query = 'select * from "addressbook" where person_id = {}'.format(person_id)
result = cur.execute(query).fetchone()
self.person_id = person_id
person_name = result[1]
person_surname = result[2]
email = result[3]
phone = result[4]
address = result[5]
# name
self.label_name = tk.Label(self.bottom, text='Name: ', font='Arial 12 bold', bg='#ebb134')
self.label_name.place(x=40, y=40)
self.entry_name = tk.Entry(self.bottom, width=30, bd=4)
self.entry_name.insert(0, person_name)
self.entry_name.place(x=175, y=40)
# surname
self.label_surname = tk.Label(self.bottom, text='Surname: ', font='Arial 12 bold', bg='#ebb134')
self.label_surname.place(x=40, y=80)
self.entry_surname = tk.Entry(self.bottom, width=30, bd=4)
self.entry_surname.insert(0, person_surname)
self.entry_surname.place(x=175, y=80)
# email
self.label_email = tk.Label(self.bottom, text='Email: ', font='Arial 12 bold', bg='#ebb134')
self.label_email.place(x=40, y=120)
self.entry_email = tk.Entry(self.bottom, width=30, bd=4)
self.entry_email.insert(0, email)
self.entry_email.place(x=175, y=120)
# phone number
self.label_phone = tk.Label(self.bottom, text='Phone Number: ', font='Arial 12 bold', bg='#ebb134')
self.label_phone.place(x=40, y=160)
self.entry_phone = tk.Entry(self.bottom, width=30, bd=4)
self.entry_phone.insert(0, phone)
self.entry_phone.place(x=175, y=160)
# address
self.label_Address = tk.Label(self.bottom, text='Address: ', font='Arial 12 bold', bg='#ebb134')
self.label_Address.place(x=40, y=200)
self.entry_Address = tk.Text(self.bottom, width=24, height=3, wrap='word')
self.entry_Address.insert(1.0, address)
self.entry_Address.place(x=175, y=200)
# submit btn
self.submit_btn = tk.Button(self.bottom, text='Update', font='Arial 12 bold', command=self.update_people)
self.submit_btn.place(x=130, y=280)
def update_people(self):
person_id = self.person_id
name = self.entry_name.get()
surname = self.entry_surname.get()
email = self.entry_email.get()
phone = self.entry_phone.get()
address = self.entry_Address.get(1.0, 'end-1c')
query = "update 'addressbook' set person_name = '{}', person_surname = '{}', person_email = '{}', person_phone = '{}', person_address = '{}' where person_id = {}".format(name, surname, email, phone, address, person_id)
try:
cur.execute(query)
con.commit()
tk.messagebox.showinfo('Success', 'Information Successfully Updated')
self.destroy()
except Exception as e:
tk.messagebox.showerror('Error', str(e))
self.destroy()
Congratulations @here-to-share! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):
Your next target is to reach 1000 upvotes.
You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Check out the last post from @hivebuzz:
Support the HiveBuzz project. Vote for our proposal!