14 Replies - 2460 Views - Last Post: 01 December 2011 - 07:19 AM Rate Topic: -----

#1 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Treeview Tkinter PostgreSQL

Posted 29 November 2011 - 06:02 AM

Hello every body , i am new in Python programming , I wanted to put my datas from a PostgreSQL database on a Treeview Tkinter interface , but i receive the error message :

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128)



My all Python code is :

#! /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()


The line where the execution is blocked is :

tv.insert("",0, values=(z[1], z[2], z[3]))


In my datas there is "Prénom1" and "Prénom2"
On the terminale the code works very good and print my datas with "é"
Thank you very much for helping me friends :)

Is This A Good Question/Topic? 0
  • +

Replies To: Treeview Tkinter PostgreSQL

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Treeview Tkinter PostgreSQL

Posted 29 November 2011 - 07:50 AM

I am unfamiliar with PostgreSQL specifically, but the error you are receiving is typical when you are working with a database that doesn't support the character set you are using. The message is telling you that you have non-ASCII character, which would be a problem if your databases character set and collation are not set appropriately.
Was This Post Helpful? 1
  • +
  • -

#3 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Treeview Tkinter PostgreSQL

Posted 29 November 2011 - 09:25 AM

Thanks Motoma for answering , so what should i do or add ? for exemple with MySQl I had the same probleme , and i puted these two commands at the end of the connexion to the database like :

charset = "utf8" , use_unicode=True


the connexion became :

db = MySQLdb.connect(host = "localhost", user = "root", passwd = "pesbakmysql",db = "testbase",charset = "utf8" , use_unicode=True)


But I couldn't do the same with :

conn = psycopg2.connect("host='localhost' dbname='firstbase' user='postgres' password='mypostgresql' ")


Thank you very much for the answer ...
Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Treeview Tkinter PostgreSQL

Posted 29 November 2011 - 09:50 AM

It seems that the connection object has a set_client_encoding() method. If this doesn't work, you may need to change the collation on your database.
Was This Post Helpful? 0
  • +
  • -

#5 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Treeview Tkinter PostgreSQL

Posted 29 November 2011 - 09:55 AM

Thank you very much Motoma , I will search about how to use the set_client_encoding() method , I don't know where to put it ??? Thanks a lot friend
Was This Post Helpful? 0
  • +
  • -

#6 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Treeview Tkinter PostgreSQL

Posted 29 November 2011 - 10:10 AM

It should go right after your connect statement, something like this:
...

def connect() :
    global conn
    conn = psycopg2.connect("host='localhost' dbname='firstbase' user='postgres' password='pesbakpostgresql'")
    conn.set_client_encoding('UTF8')

...


Was This Post Helpful? 0
  • +
  • -

#7 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Treeview Tkinter PostgreSQL

Posted 30 November 2011 - 02:30 AM

Good morning Motoma , thank you very much for the solution , i just will try it now , i'll tell what will happen .
Was This Post Helpful? 0
  • +
  • -

#8 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Treeview Tkinter PostgreSQL

Posted 30 November 2011 - 02:37 AM

:( , I've just tried the solution but it didn't work , I always have the error message :

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128)



It think it is something that have relation with the Treeview , because it prints on the Terminal , and as text in a Label ...
Was This Post Helpful? 0
  • +
  • -

#9 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Treeview Tkinter PostgreSQL

Posted 30 November 2011 - 07:12 AM

Would you mind posting the full traceback? I want to see where things are getting caught up.
Was This Post Helpful? 0
  • +
  • -

#10 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Treeview Tkinter PostgreSQL

Posted 30 November 2011 - 09:00 AM

Thank friend , ok this is it :

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
  File "./TkDbPostgresql.py", line 85, in insere
    cur.execute("INSERT INTO firstbasetable ( Prenom, Nom, Phone) VALUES (%s, %s, %s)",(firstnamevar, lastnamevar, Phonevar ))
ProgrammingError: ERREUR:  la colonne « prenom » de la relation « firstbasetable » n'existe pas
LINE 1: INSERT INTO firstbasetable ( Prenom, Nom, Phone) VALUES (E't...



Helllo Motoma :) , I'm sorry :) , I have put a wrong message erreur , this is the right one :

ismail@pbadmin-desktop:~/Bureau$ ./TkDbPostgresql1.py 
Prénom
Prénom1              : Nom1                
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
  File "./TkDbPostgresql1.py", line 30, in afficher
    tv.insert("","end", values=(z[1], z[2], z[3]))
  File "/usr/local/lib/python2.6/dist-packages/ttk.py", line 1370, in insert
    opts = _format_optdict(kw)
  File "/usr/local/lib/python2.6/dist-packages/ttk.py", line 80, in _format_optdict
    v.append(_text(val) if val else '{}')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128)


Was This Post Helpful? 0
  • +
  • -

#11 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Treeview Tkinter PostgreSQL

Posted 30 November 2011 - 09:02 AM

That seems to be a different error...
Was This Post Helpful? 0
  • +
  • -

#12 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Treeview Tkinter PostgreSQL

Posted 30 November 2011 - 09:08 AM

hhh :) , but the last one , is correct .i'm sorry
Was This Post Helpful? 0
  • +
  • -

#13 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Treeview Tkinter PostgreSQL

Posted 30 November 2011 - 09:24 AM

This is not a PostgrSQL issue, it is actually a problem with Tkinter. Looking at the version of Tkinter that is installed with Python 2.7, the code throwing your error properly decodes Unicode strings. Unfortunately, this is likely a issue with your version of Tkinter.

My suggestion would be to upgrade to Python 2.7 and see if that fixes your problem.
Was This Post Helpful? 0
  • +
  • -

#14 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Treeview Tkinter PostgreSQL

Posted 30 November 2011 - 09:34 AM

Thanks very much for your efforts , u know ? i use Python3.1 maybe , will , ok forget about that :) , i will change this :) , good luck .
Was This Post Helpful? 0
  • +
  • -

#15 Ismatus2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 29-November 11

Re: Treeview Tkinter PostgreSQL

Posted 01 December 2011 - 07:19 AM

U know , I tried my treeview with out data base , and i wanted to insert 'é' for exemple and i have the same message error :

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128)



I putted :

tv.insert("","end", values=("%s %s %s"('é', 'é', 'é'))


Thank u for answering
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1