هذا هو الكود كامل:
from tkinter import *
from tkinter import ttk ,Tk
import pymysql
import tkinter as tk
class Student:
#========انشاء نافذة البرنامج========
def __init__(self , root):
self.root = root
self.root.geometry('1350x690+1+1')
self.root.resizable(False,False)
self.root.title('برنامج دليفري شوب')
self.root.configure(background="silver")
title = Label(self.root,text=' إدارة شئون العاملين ',bg='silver',font=('monospace',18,'bold','underline'),background='blue',fg='white')
title.pack(fill=X)
#===============المتغيرات===============
self.id_var = StringVar()
self.name_var = StringVar()
self.nid_var = StringVar()
self.phone_var = StringVar()
self.address_var = StringVar()
self.jobdate_var = StringVar()
self.job_var = StringVar()
self.hub_var = StringVar()
self.salary_var = StringVar()
self.delete_var = StringVar()
self.seVar = StringVar()
self.seBy=StringVar()
search_type_var = StringVar
#===========ادوات التحكم بالبرنامج========
Manage_Frame = Frame(self.root,bg='white')
Manage_Frame.place(x=1135,y=40,width=210,height=550)
lbl_ID = Label(Manage_Frame,text='الكود',bg='white',font=('tajawal',15,'bold'))
lbl_ID.pack()
ID_Entry = Entry(Manage_Frame,textvariable=self.id_var,bd=2,font=('tajawal',13),fg='blue',justify='center')
ID_Entry.pack()
lbl_Name = Label(Manage_Frame,text='الاسم',bg='white',font=('tajawal',15,'bold'))
lbl_Name.pack()
Name_Entry = Entry(Manage_Frame,textvariable=self.name_var,bd=2,font=('tajawal',13),fg='blue',justify='center')
Name_Entry.pack()
lbl_NID = Label(Manage_Frame,text='الرقم القومي',bg='white',font=('tajawal',15,'bold'))
lbl_NID.pack()
NID_Entry = Entry(Manage_Frame,textvariable=self.nid_var,bd=2,font=('tajawal',13),fg='blue',justify='center')
NID_Entry.pack()
lbl_Phone = Label(Manage_Frame,text='رقم التليفون',bg='white',font=('tajawal',15,'bold'))
lbl_Phone.pack()
Phone_Entry = Entry(Manage_Frame,textvariable=self.phone_var,bd=2,font=('tajawal',13),fg='blue',justify='center')
Phone_Entry.pack()
lbl_Add = Label(Manage_Frame,text='العنوان',bg='white',font=('tajawal',15,'bold'))
lbl_Add.pack()
Add_Entry = Entry(Manage_Frame,textvariable=self.address_var,bd=2,font=('tajawal',13),fg='blue',justify='center')
Add_Entry.pack()
lbl_Date = Label(Manage_Frame,text='تاريخ التعيين',bg='white',font=('tajawal',15,'bold'))
lbl_Date.pack()
Date_Entry = Entry(Manage_Frame,textvariable=self.jobdate_var,bd=2,font=('tajawal',13),fg='blue',justify='center')
Date_Entry.pack()
lbl_Job = Label(Manage_Frame,text='الوظيفة',bg='white',font=('tajawal',15,'bold'))
lbl_Job.pack()
Combo_Job = ttk.Combobox (Manage_Frame,textvariable=self.job_var)
Combo_Job['value']=('مدير','مشرف','محاسب','مندوب','اوفيس بوي')
Combo_Job.pack()
lbl_Hub = Label(Manage_Frame,text='الفرع',bg='white',font=('tajawal',15,'bold'))
lbl_Hub.pack()
Hub_Entry = Entry(Manage_Frame,textvariable=self.hub_var,bd=2,font=('tajawal',13),fg='blue',justify='center')
Hub_Entry.pack()
lbl_Salary = Label(Manage_Frame,text='المرتب',bg='white',font=('tajawal',15,'bold'))
lbl_Salary.pack()
Salary_Entry = Entry(Manage_Frame,textvariable=self.salary_var,bd=2,font=('tajawal',13),fg='blue',justify='center')
Salary_Entry.pack()
lbl_Delete = Label(Manage_Frame,fg='red',bg='white',text='حذف بالأسم',font=('tajawal',15,'bold'))
lbl_Delete.pack()
Delete_Entry = Entry(Manage_Frame,textvariable=self.delete_var,bd=2,font=('tajawal',13),fg='blue',justify='center')
Delete_Entry.pack()
#=========buttons=========
btn_Frame = Frame(self.root,bg='white')
btn_Frame.place(x=1137,y=595,width=210,height=200)
add_btn = Button(btn_Frame,text='اضافة',bg='#0B41F8',fg='white',font=('tajawal',11,'bold'),command=self.add_student)
add_btn.place(x=105,y=3,width=100,height=25)
del_btn = Button(btn_Frame,text='حذف',bg='#0B41F8',fg='white',font=('tajawal',11,'bold'),command=self.delete)
del_btn.place(x=5,y=30,width=90,height=25)
update_btn = Button(btn_Frame,text='تعديل',bg='#0B41F8',fg='white',font=('tajawal',11,'bold'),command=self.update)
update_btn.place(x=5,y=3,width=90,height=25)
clear_btn = Button(btn_Frame,text='افراغ الحقول',bg='#0B41F8',fg='white',font=('tajawal',11,'bold'),command=self.clear)
clear_btn.place(x=105,y=30,width=100,height=25)
exit_btn = Button(btn_Frame,text='خروج',bg='#0B41F8',fg='white',font=('tajawal',11,'bold'),command=quit)
exit_btn.place(x=10,y=60,width=190,height=30)
#=================search manage ==========================
search_frame = Frame(self.root,bg='white')
search_frame.place(x=1,y=40,width=1133,height=50)
lbl_search = Label(search_frame,text='بحث',bg='white',font=('arial',14,'bold'))
lbl_search.place(x=1090,y=12)
#search_type_var = tk.StringVar(value="الكود")
#search_type_options = ['التليفون', 'الاسم', 'الوظيفة','الفرع']
#search_type_dropdown = tk.OptionMenu(search_frame, search_type_var, *search_type_options)
#search_type_dropdown.place(x=1005,y=13)
combo_search = ttk.Combobox(search_frame,justify='center')
combo_search['value']=('الاسم','التليفون','الوظيفة','الفرع')
combo_search.place(x=940,y=12)
search_Entry = Entry(search_frame,textvariable=self.seVar,justify='center',bd=2,font=('tajawal',13),fg='blue')
search_Entry.place(x=750,y=12)
se_btn = Button(search_frame,text='بحث',bg='#0B41F8',fg='white',font=('tajawal',11,'bold'),command=self.search)
se_btn.place(x=670,y=12,width=70,height=25)
pr_btn = Button(search_frame,text='طباعة',bg='#0B41F8',fg='white',font=('tajawal',11,'bold'))
pr_btn.place(x=300,y=12,width=90,height=30)
intag_btn = Button(search_frame,text='الانتاجيات',bg='#0B41F8',fg='white',font=('tajawal',11,'bold'))
intag_btn.place(x=200,y=12,width=90,height=30)
acount_btn = Button(search_frame,text='الحسابات',bg='#0B41F8',fg='white',font=('tajawal',11,'bold'))
acount_btn.place(x=100,y=12,width=90,height=30)
exit1_btn = Button(search_frame,text='خروج',bg='#0B41F8',fg='white',font=('tajawal',11,'bold'),command=quit)
exit1_btn.place(x=5,y=12,width=90,height=30)
#==============dietals=======
Dietals_Frame = Frame(self.root)
Dietals_Frame.place(x=3,y=92,width=1128,height=593)
#=========scroll========
scroll_x = Scrollbar(Dietals_Frame,orient=HORIZONTAL)
scroll_y = Scrollbar(Dietals_Frame,orient=VERTICAL)
#==========treeview==========
self.student_table = ttk.Treeview(Dietals_Frame,
columns=('salary','hub','job','jobdate','address','phone','NID','name','id'),
xscrollcommand=scroll_x.set,
yscrollcommand=scroll_y.set)
self.student_table.place(x=18,y=1,width=1120,height=570)
scroll_x.pack(side=BOTTOM,fill=X)
scroll_y.pack(side=LEFT,fill=Y)
self.student_table['show']='headings'
self.student_table.heading('salary',text='المرتب')
self.student_table.heading('hub',text='الفرع')
self.student_table.heading('job',text='الوظيفة')
self.student_table.heading('jobdate',text='تاريخ التعيين')
self.student_table.heading('address',text='العنوان')
self.student_table.heading('phone',text='التليفون')
self.student_table.heading('NID',text='الرقم القومي')
self.student_table.heading('name',text='الاسم')
self.student_table.heading('id',text='الكود')
self.student_table.column('salary',width=20)
self.student_table.column('hub',width=50)
self.student_table.column('job',width=50)
self.student_table.column('jobdate',width=60)
self.student_table.column('address',width=320)
self.student_table.column('phone',width=60)
self.student_table.column('NID',width=90)
self.student_table.column('name',width=170)
self.student_table.column('id',width=30)
self.student_table.bind('<ButtonRelease-1>',self.get_cursor)
#============con +add============
self.fetch_all()
def add_student(self):
con = pymysql.connect(
host='localhost',
user='root',
password='',
database='abozaid'
)
cur = con.cursor()
cur.execute("insert into employee value(%s,%s,%s,%s,%s,%s,%s,%s,%s)",(
self.salary_var.get(),
self.hub_var.get(),
self.job_var.get(),
self.jobdate_var.get(),
self.address_var.get(),
self.phone_var.get(),
self.nid_var.get(),
self.name_var.get(),
self.id_var.get()
))
con.commit()
self.fetch_all()
self.clear()
con.close()
def fetch_all(self):
con = pymysql.connect(host='localhost',user='root',passwd='',database='abozaid')
cur = con.cursor()
cur.execute('select * from employee')
rows = cur.fetchall()
if len(rows) !=0:
self.student_table.delete(*self.student_table.get_children())
for row in rows:
self.student_table.insert('',END,values=row)
con.commit()
con.close()
def delete(self):
con = pymysql.connect(host='localhost',user='root',passwd='',database='abozaid')
cur = con.cursor()
cur.execute('delete from employee where name=%s',self.delete_var.get())
con.commit()
self.fetch_all()
con.close()
def clear(self):
self.id_var.set('')
self.name_var.set('')
self.nid_var.set('')
self.phone_var.set('')
self.address_var.set('')
self.jobdate_var.set('')
self.job_var.set('')
self.hub_var.set('')
self.salary_var.set('')
def get_cursor(self,ev):
cursor_row = self.student_table.focus()
contens = self.student_table.item(cursor_row)
row = contens['values']
self.id_var.set(row[8])
self.name_var.set(row[7])
self.nid_var.set(row[6])
self.phone_var.set(row[5])
self.address_var.set(row[4])
self.jobdate_var.set(row[3])
self.job_var.set(row[2])
self.hub_var.set(row[1])
self.salary_var.set(row[0])
def update(self):
con = pymysql.connect(
host='localhost',
user='root',
password='',
database='abozaid')
cur = con.cursor()
cur.execute("update employee set salary=%s,hub=%s,job=%s,jobdate=%s,address=%s,phone=%s,nid=%s,name=%s where id=%s",(
self.salary_var.get(),
self.hub_var.get(),
self.job_var.get(),
self.jobdate_var.get(),
self.address_var.get(),
self.phone_var.get(),
self.nid_var.get(),
self.name_var.get(),
self.id_var.get()
))
con.commit()
self.fetch_all()
self.clear()
con.close()
def search(self):
con = pymysql.connect(host='localhost',user='root',passwd='',database='abozaid')
cur = con.cursor()
cur.execute("SELECT * FROM employee WHERE " + (self.seBy.get())+ "LIKE'%" + str(self.seVar.get()) + "%'")
rows = cur.fetchall()
if len(rows) !=0:
self.student_table.delete(*self.student_table.get_children())
for row in rows:
self.student_table.insert("",END,values=row)
con.commit()
con.close()
root = Tk()
ob = Student(root)
root.mainloop()