I have a python code , i want delete the item that appears in the Treeview from my database , using a Button : "Supprimer" :
but i still could not get variables from treeview , or I can't tell the programme the variables that are selected in the treeview this is my code :
#! /usr/bin/python
#-*- coding: latin-1-*-
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 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"%(z[1].decode('utf-8'), z[2].decode('utf-8'), z[3].decode('utf-8'))))
lab6.config(text = "%s"%B)/>
listb.insert(END, z[1])
# La fonction de connexion
def connect() :
global conn
conn = psycopg2.connect("host='localhost' dbname='firstbase' user='postgres' password='bakpostgresql' ")
# La fonction supprimmer ( here where i need to know what to put )
def supprimer :
connect()
cur = conn.cursor()
cur.execute("""DELETE FROM phonebook WHERE WHERE Prenom = '%s' AND Nom = '%s'""",%(firstname_selected, lastnameselected))
# 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)
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("")
# The Scrollbar
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"]=("col1","col2","col3")
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("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)
# 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 = supprimer)
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()
Thank you for answering

New Topic/Question
Reply


MultiQuote

|