1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #! /usr/bin/python
#-*- coding:Utf-8 -*-
from Tkinter import *
import ttk
import psycopg2
def center(window):
sw = window.winfo_screenwidth()
sh = window.winfo_screenheight()
rw = window.winfo_reqwidth()
rh = window.winfo_reqheight()
xc = (sw - rw) / 2
yc = (sh -rh) / 2
window.geometry("+%d+%d" % (xc, yc))
window.deiconify()
# La fonction inserer ou afficher
def afficher() :
connect()
cur = conn.cursor()
cur.execute("set client_encoding to 'UTF8'; SELECT * from firsttable")
rows = cur.fetchall()
if rows :
b = StringVar()
b = ""
for z in rows:
b += z[1].strip() + " " + "|" + " " + z[2].strip() + " " + "|" + " " + z[3].strip() + "\n"
print b
tv.insert("","end", values=("%s %s %s %s"%(z[0], z[1].decode('utf-8'), z[2], z[3])))
lab6.config(text = "%s"%b)
listb.insert(END, z[1])
cur.close()
conn.commit()
def on_click(event):
seltxt = tv.set(tv.selection(), 'Id') or 'None'
print seltxt.encode('utf-8')
lab7.configure(text="L'id selectioné est : %s"%seltxt.encode('utf-8'))
connect()
cur = conn.cursor()
cur.execute("""DELETE FROM firsttable WHERE Idpersonnes = '%s' """%seltxt)
# La connexion à la base de données
def connect() :
global conn
conn = psycopg2.connect("host='localhost' dbname='firstbase' user='postgres' password='bakpostgresql' ")
#def itemremove():
def item_delete(event):
connect()
cur = conn.cursor()
tv = event.widget
iid = tv.focus()
if iid.startswith('it.'):
ix = int(iid[3:]) # get record id from iid
assert ix >= 0 and ix < len(ROWS), 'fatal: %d not in range, iid=%s' % (ix, iid)
del ROWS[ix]
tv.delete(iid)
# Linterface :
fenetre=Tk()
fenetre.geometry("700x400")
fenetre.title(' Test Data')
firstnamevar = StringVar()
lastnamevar = StringVar()
Phonevar = IntVar()
f1 = Frame(fenetre, bg="#290080", width=500, height=500)
f1.pack( fill=X, expand=0)
f2 = Frame(fenetre, bg="white", width=215, height=135)
f2.place(x=130 , y=213)
lab1 = Label(fenetre, text="Voila la table testtable de la base testbase" , bg = "#290080", fg = "white" )
lab1.place ( x=100 , y=15 )
lab2 = Label(fenetre, text="Prénom" , bg = "#290080", fg = "white" )
lab2.place ( x=30 , y=100 )
lab3 = Label(fenetre, text="Nom" , bg = "#290080", fg = "white" )
lab3.place ( x=30 , y=125)
lab4 = Label(fenetre, text="Phone" , bg = "#290080", fg = "white" )
lab4.place ( x=30 , y=150)
lab5 = Label(fenetre, text="Données : " , bg = "#290080", fg = "white" , anchor=W, justify=LEFT)
lab5.place ( x=30 , y=220)
lab6 = Label(fenetre, text="" , bg = "white", fg = "#290080" , anchor=W, justify=LEFT)
lab6.place ( x=130 , y=220)
lab7 = Label(fenetre, text="L'id selectioné est : " , bg = "#290080", fg = "white")
lab7.place ( x=450 , y=220)
listb=Listbox(fenetre, fg = "#290080", width = "10")
listb.place(x = 380, y = 215 )
listb.insert(END, " Prénom ")
listb.itemconfig(0, fg = "black")
# Les entrées
firstname_entry = ttk.Entry(fenetre, width=15, textvariable=firstnamevar)
firstname_entry.place (x = 100 , y = 100 )
lastname_entry = ttk.Entry(fenetre, width=15, textvariable=lastnamevar)
lastname_entry.place (x = 100 , y = 125 )
Phone_entry = ttk.Entry(fenetre, width=15, textvariable=Phonevar)
Phone_entry.place (x = 100 , y = 150 )
Phonevar.set("")
scrollbar = Scrollbar(fenetre)
scrollbar.place (x = 670 , y = 170)
# la creation de la TreeView
tv = ttk.Treeview(fenetre, show='headings', height =3, yscrollcommand=scrollbar.set)
tv["columns"]=("Id","col1","col2","col3")
tv.column("Id",width=80,anchor="center", stretch = True, minwidth = 50)
tv.column("col1",width=80,anchor="center", stretch = True, minwidth = 50)
tv.column("col2",width=80,anchor="center")
tv.column("col3",width=110,anchor="center")
tv.heading("Id",text="Id")
tv.heading("col1",text="Prénom")
tv.heading("col2",text="Nom")
tv.heading("col3",text="Phone number")
tv.place( x = 400 , y = 120)
scrollbar.config(command=tv.yview)
#tv.bind("<Button-1>", on_click)
#tv.bind("<Button-3>", on_click)
#tv.tag_bind('mb3_click', '<3>', on_click)
# Les bouttons
afficher = Button(fenetre, text = "Afficher" , command = afficher)
afficher.place( x=290, y=80 )
inserer = Button(fenetre, text = "Inserer" )
inserer.place( x=290, y=110 )
modifier = Button(fenetre, text = "Modifier" )
modifier.place( x=290, y=140 )
supprimer = Button(fenetre, text = "Supprimer" , command=lambda: on_click(None))
supprimer.place( x=290, y=170 )
Quitter = Button(fenetre, text = "Quitter" , command = fenetre.quit)
Quitter.place( x=200, y=350 )
fenetre.after(0,center,fenetre)
fenetre.mainloop() |
Partager