j'ai enfin pu connecté l'interface Tkinter avec ma base PostgreSQL , j'ai mis ce code :
#! /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()
def afficher() :
connect()
cur = conn.cursor()
cur.execute("""SELECT * from firstbasetable""")
rows = cur.fetchall()
if rows :
for z in rows:
print z[1] + ' : ' + str(z[2])
tv.insert("","end", values=(z[1], z[2], z[3]))
def connect() :
global conn
conn = psycopg2.connect("host='localhost' dbname='firstbase' user='postgres' password='pesbakpostgresql'")
fenetre=Tk()
fenetre.geometry("500x400")
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)
lab1 = Label(fenetre, text="Voila la table testtable de la base testbase" , bg = "#290080", fg = "white" )
lab1.place ( x=100 , y=15 )
lab1 = Label(fenetre, text="Prénom" , bg = "#290080", fg = "white" )
lab1.place ( x=30 , y=100 )
lab1 = Label(fenetre, text="Nom" , bg = "#290080", fg = "white" )
lab1.place ( x=30 , y=125)
lab1 = Label(fenetre, text="Phone" , bg = "#290080", fg = "white" )
lab1.place ( x=30 , y=150)
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 412 , y 301 )
tv = ttk.Treeview(fenetre, show='headings', height =3, yscrollcommand=scrollbar.set)
tv["columns"]=("col1","col2","col3")
tv.column("col1",width=100,anchor="center", stretch = True, minwidth = 50)
tv.column("col2",width=100,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 100 , y 250 )
scrollbar.config(command=tv.yview)
afficher Button(fenetre, text "Afficher" , command = afficher)
afficher.place( x=315, y=80 )
inserer Button(fenetre, text "Inserer" , command = insere)
inserer.place( x=315, y=110 )
modifier Button(fenetre, text "Modifier" ) #, command = modif)
modifier.place( x=315, y=140 )
supprimer Button(fenetre, text "Supprimer" , command = afficheselected)
supprimer.place( x=315, y=170 )
Quitter Button(fenetre, text "Quitter" , command = fenetre.quit)
Quitter.place( x=200, y=350 )
fenetre.after(0,center,fenetre)
fenetre.mainloop()
ça marche bien , sauf que dans la fonction afficher() , quand j'insere les données dans la Treeview j'ai le message d'erreur suivant :
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128)
Et si j'affiche les données sur un terminale c'est bien : car j'ai dans la bas Prénom1 , Prénom2 dans un champ "Prenom" de la table "firstbasetable " , je pense que le probleme vient du manque des deux commandes :
charset = "utf8" , use_unicode=True
qui doivent etre dans la définition de la connexion comme dans mysql , mais je sais pas comment les placer avec psycopg2 , sur MySQL c'est comme ça :
db MySQLdb.connect(host "localhost", user = "root", passwd = "firstmysql",db = "testbase",charset = "utf8" , use_unicode=True)
J'arrive pas à en sortir encore ...
Afficher la suite