You are on page 1of 46

University of basrah

College of IT
Department of information systems

Advanced Payroll
Management System
Using The Python Language
Lecture By Dr: Haider M. Al-mashhadi

)‫نور طه‬,‫ياسين يعقوب‬,‫(وليد خالد‬:‫طالب‬


Contents

1. Introduction of programming.

2.Import tkinter and create Form.

3.Design the frame

4. Button and database (MySql)


1. Introduction of programming
2.Import tkinter and create Form.

• Before to create form we need first to


import library tkinter to the project

• Then in class we set the title


and the geometry of the
project and the background.
• And in main we define TK function and
class Payroll to create and running the
form
3.Design the frame
3.Design the frame

We know Label as Title in TopFrame1 and inside the brackets we know the following:
• font(_FontDescription)
• Text(Str)
• Bd(_ScreenUnits),
• Bg(background _Color),
• justify (Literal['left', 'center', 'right’])
3.Design the frame

-In topFrame2 we have 4 label and 3 Entry and 1


combo box

To insert combo box we have to import TTK from


tkinter library and make state ‘readonly’ to make
sure that cant change the text value that we put
inside combo box.
-And for inside the brackets we know the
following:
• text variable( Variable)
• fg(font color)
• Anchor(to define where text is positioned
relative to a reference point)
3.Design the frame

-In this frame we have treeview(Table),that


have all information about Employer. get it
from Database(MySQL).
-we have 11 Columns that all Column
Have (
column(
column: Tree view Column Id,
width: int = ...,
\anchor: _Anchor = CENTER)
)
-And text for heading of treeview(table)
4. Button and database (MySql)

• Add to SQL
4. Button and database (MySql)

• Update to SQL
4. Button and database (MySql)

• Delete to SQL
4. Button and database (MySql)

• Select to SQL
End of Project
SQLite
◦ SQL stands for “Structured Query Language” and is the main language that the large
database packages use. SQLite is free software that can be used as an SQL database.
You can download the latest version of the software from www.sqlite.org.
◦ To use SQL you need to load the “DB Browser for SQLite” which you can download from
https://sqlitebrowser.org.
Understanding a Relational
Database
◦ We will use the example of a small
manufacturing company that stores
the details of their employees in an
SQL database.
◦ Below is an example of the Employees
table, which contains the details of all
the employees in the company. The
contents of a table can be viewed by
clicking on the Browse Data tab.
◦ It has four fields (ID, Name, Dept and
Salary) and 10 records in it (one for each
employee). Take a look at the employees
list and you will notice that more than one
employee is listed in the same department.
◦ In most databases you would find repetitive
data such as this. To make the database
work more efficiently the repeated data
are often stored in a separate table. In this
case there is a department table which
would store all the information about each
department to save having to repeat all
the department details for each employee.
◦ By splitting the data into two tables like this, if we need to update the
manager it only needs to be updated in one place rather than updating it
several times, which we would need to do if it was all stored in a single table.
◦ This is known as a one-to-many relationship as one department can have
many employees in it.
◦ A primary key is the field (usually the first one) in each table that stores the
unique identifier for that record. Therefore, in the Employees table the primary
key will be the ID column and in the Department table the primary key will be
Dept.
◦ When creating a table, you need to identify the following for each field:
◦ the name of the field (field names cannot contain spaces and must
follow the same rules as variable names);
◦ if it is a primary key;
◦ the data type for that field.
◦ The data types you can use are as follows:
◦ integer: the value is an integer value;
◦ real: the value is a floating-point value;
◦ text: the value is a text string;
◦ blob: the value is stored exactly as it was input.
◦ You can also specify if the field cannot be left blank by adding NOT
NULL to the end of the field when you create it.
Example Code
Exercise (139)
import sqlite3
with sqlite3.connect('PhoneBook.db') as db:
cursor=db.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS Names(
id integer PRIMARY KEY,
firstName text,
surName text,
phoneNumber text);""")

cursor.execute("""INSERT INTO
Names(id,firstName,surName,phoneNumber)
VALUES("1","Ahmed","Basray", "0770313131")""")
db.commit()
cursor.execute("""INSERT INTO
Names(id,firstName,surName,phoneNumber)
VALUES("2","Ali","Baghdadi", "0783454545")""")
db.commit()
cursor.execute("""INSERT INTO
Names(id,firstName,surName,phoneNumber)
VALUES("3","Hussain","Karbalaiee", "0780767676")""")
db.commit()

db.close()
#Exercize 140 with sqlite3.connect('PhoneBook.db') as db:
import sqlite3 cursor = db.cursor()
def main():
def viewphonebook(): again="y"
cursor.execute("SELECT * FROM Names") while again=="y":
for x in cursor.fetchall(): print()
print(x) print("Main Menu")
print()
def addtophonebook(): print("1) Veiw phone book")
newid=input("EnterID: ") print("2) Add to phone book")
newfname=input("Enter first name: ") print("3) Search for surname")
newsname=input("Enter surname: ") print("4) Delete person from phone book")
newpnum=input("Enter phone number: ") print("5) Quit")
cursor.execute("""INSERT INTO Names(id,firstname,surname,phone_number) print()
VALUES(?,?,?,?)""",(newid,newfname,newsname,newpnum)) selection=input("Enter your selection: ")
db.commit print()
def selectname(): if selection==1:
selectsname=input("Enter a surname: ") viewphonebook()
cursor.execute("SELECT * FROM Names WHERE surname=?",[selectsname]) elif selection==2:
for x in cursor.fetchall(): addtophonebook()
print(x) elif selection==3:
selectname()
def deletedata(): elif selection==4:
selectid=input("Enter ID: ") deletedata()
cursor.execute("DELETE FROM Names WHERE ID=?",[selectid]) elif selection==5:
cursor.execute("SELECT * FROM Names") again = "n"
for x in cursor.fetchall(): else:
print(x) print("Incorrect selection entered")

main()
db.close()
Tkinter GUI

Dr. Haider M. Al-Mashhadi


Tkinter GUI: Graphical User Interface
◦ A GUI (graphical user interface)
makes the program easier to
use. It allows you, as the
programmer, to create screens,
text boxes and buttons to help
the user navigate through the
program in a more user-friendly
way. Tkinter is a library of
features in Python that allows
you to do this.
◦ Look at the code below and in
particular the measurements
that are used in the
window.geometry and
button.place lines.
◦ Now look at the window that this code will produce:

◦ The geometry line in the code determines the size of


the window and the place line in the code determines
the position of the individual item on the window.

◦ Once the button is pressed it will run the “Call”


subprogram and change the window to look as
follows:
Example Code:
import tkinter as tk
#from tkinter import *

window = tk.Tk()
label = tk.Label(text="University of Basrah!")
label.pack()
window.mainloop()
import tkinter as tk

# Button
window = tk.Tk()
button = tk.Button(width=25, height=25,
bg="white", fg="blue", text="Click here")
button.pack()
window.mainloop()

# TextBox (Entry)
window = tk.Tk()
entry = tk.Entry(width=40)
entry.insert(0, "What is your name?")
entry.pack()
window.mainloop()
import random
# Roll Application
import tkinter as tk
import random
def change_color():
import tkinter as tk
color = random.choice(["red", "orange",
def roll():
"yellow", "blue", "green", "indigo", "violet"])
lbl_result["text"] = str(random.randint(1,
button["bg"] = color
6))
color = random.choice(["red", "orange",
window = tk.Tk()
"yellow", "blue", "green", "indigo", "violet"])
window.columnconfigure(0, minsize=150)
button["fg"] = color
window.rowconfigure([0, 1], minsize=50)
btn_roll = tk.Button(text="Roll",
window = tk.Tk()
command=roll)
window.geometry("300x200")
lbl_result = tk.Label()
button = tk.Button(text="Click me",
btn_roll.grid(row=0, column=0,
command=change_color)
sticky="nsew")
button.place(x=40,y=100,width=200,height
lbl_result.grid(row=1, column=0)
=30)
window.mainloop()
window.mainloop()
from tkinter import *

def call():
msg=Label(window, text="You Clicked the
button..")
msg.place(x=30,y=50)
button["bg"]="blue"
button["fg"]="white"

window=Tk()
window.geometry("200x110")
window.title("My First GUI program..")
button=Button(text="Click Here",
command=call)
button.place(x=30,y=20,width=120,height=25)

window.mainloop()
from tkinter import *
def click():
name=textbox1.get() from tkinter import *
message=str("Hello "+" "+ name)
textbox2["bg"]="yellow" import random
textbox2["fg"]="blue" def click():
textbox2["text"]=message
num=random.randint(1,6)
window=Tk() answer["text"]=num
window.geometry("500x200")
window.title("GUI program..")
window=Tk()
label1=Label(text="Enter your name: ") window.title("Role a Dice")
label1.place(x=30,y=20, width=120,height=25)
window.geometry("200x160")
textbox1=Entry(text="") button1=Button(text="Roll", command=click)
textbox1.place(x=180,y=20,width=200,height=25)
textbox1["justify"]="center" button1.place(x=30,y=30,width=50,height=25)
textbox1.focus() answer=Message(text="")
button1=Button(text="Press me",command=click)
answer.place(x=40,y=70,width=30,height=75)
button1.place(x=30,y=50,width=120,height=25)
window.mainloop()
textbox2=Message(text="")
textbox2.place(x=180,y=50,width=200,height=25)
textbox2["bg"]="white"
textbox2["fg"]="black"

window.mainloop()
from tkinter import *
def add_on(): enter_txt=Entry(text=0)
num=enter_txt.get() enter_txt.place(x=150, y=20, width=100, height=25)
num=int(num) enter_txt["justify"]="center"
answer=output_txt["text"] enter_txt.focus()
answer=int(answer)
total=num+answer add_btn=Button(text="Add", command=add_on)
output_txt["text"]=total add_btn.place(x=300,y=20,width=100,height=25)

output_lbl=Label(text="Answer= ")
def reset(): output_lbl.place(x=50,y=50,width=100,height=25)
total=0
output_txt["text"]=0 output_txt=Message(text=0)
enter_txt.delete(0,END) output_txt.place(x=150, y=50, width=100,height=25)
enter_txt.focus() output_txt["bg"]="white"
output_txt["relief"]="sunken"
total=0 clear_btn=Button(text="Clear", command=reset)
clear_btn.place(x=300, y=50,width=50,height=25)
num=0
window.mainloop()
window=Tk()
window.title("Adding Together")
window.geometry("450x100")
enter_lbl=Label(text="Enter a number: ")
enter_lbl.place(x=50,y=20,width=100, height=25)
ListBox Widget
lb = Listbox(ws)
from tkinter import * lb.pack()
lb.insert(0, 'red')
ws = Tk() lb.insert(1, 'green')
lb.insert(2, 'yellow')
ws.title('Python ListBox..') lb.insert(3, 'blue')
ws.geometry('400x300')
Button(ws, text='Show Selected',
ws.config(bg='#446644') command=showSelected).pack(p
ady=20)
def showSelected(): show = Label(ws)
show.pack()

show.config(text=lb.get(AN ws.mainloop()
CHOR))
from tkinter import *

ws = Tk() show = Label(ws, text = "Select Your Country", font = ("Times",


ws.title('Python ListBox..') 14), padx = 10, pady = 10)
show.pack()
ws.geometry('400x300') lb = Listbox(ws, selectmode = "multiple")
lb.pack(padx = 10, pady = 10, expand = YES, fill = "both")
var = StringVar()
x =["Afghanistan", "Albania", "Algeria", "Andorra", "Angola",
"Australia", "Brazil", "Canada", "China", "Iceland", "Israel",
def showSelected(): "United States", "Zimbabwe"]
countries = []
for item in range(len(x)):
cname = lb.curselection() lb.insert(END, x[item])
for i in cname: lb.itemconfig(item, bg="#bdc1d6")
op = lb.get(i)
Button(ws, text="Show Selected",
countries.append(op) command=showSelected).pack()
for val in countries: ws.mainloop()
print(val)
from tkinter import *

ws = Tk() lb.insert(0, 'Iceland')


ws.title('Python ListBox..') lb.insert(1, 'USA')
lb.insert(2, 'China')
ws.geometry('400x300')
lb.insert(3, 'Europe')
ws.config(bg='#446644')
disp = Label(ws,
def showSelected():
textvariable=var)
itm = lb.get(lb.curselection()) disp.pack(pady=20)
var.set(itm) Button(ws, text='Show
Selected',
command=showSelected).pa
var =StringVar() ck()
lb = Listbox(ws)
lb.pack() ws.mainloop()

You might also like