Hello ,
I'm working to build a programme that connects to MySQL database using Tkinter .In a step , i wanted to create dynamicly a number of entries widgets ( here : tailles_entry[i] ) depending on the number of elements of a list ( here liste1 ) . I could create that entries ( function : valider() ), but i couldn't insert the texts entered into the database ( function valider1() ) .
I show here all the code of the window :

def Interface2() :

    global interf2
    interf2 = Tk()    
    interf2.geometry("500x400")
    interf2.title('                                Gestion du Depot ')  

    f1 = Frame(interf2, bg="blue",  width=500, height=700)
    f1.pack( fill=X, expand=0)

    lab2 = Label(interf2, text="Veuillez saisir le numero d'Import du Model ( num+année , ex: 122013 )\n et cliquer sur valider " , bg = "blue", fg = "white" )
    lab2.place ( x=80 , y=45 )


    global liste1
    global distligne
    global modelidvar
    global numbonvar
    global qtetaillevar


    modelidvar = StringVar()
    numbonvar = StringVar()
    qtetaillevar = StringVar()

    qtetaillevar = {}

    distligne = 0
    liste1 = []


    lab3= Label(interf2, text="Référence Model :" , bg = "blue", fg = "white" )
    lab3.place ( x=30 , y=100)
    modelid_entry = ttk.Entry(interf2, width=13, textvariable=modelidvar)
    modelid_entry.place (x = 140 , y = 100 )
    tailles_entry = ttk.Entry(interf2, width=13)


    def revenir():                      
           interf2.destroy()
           InterfaceDeps()      
    def valider1():
           print "valider1"
           connect()
           curs = conn.cursor()
           curs.execute(' select client.*, model.* from client,model where client.clientid=model.client AND modelid=%s ;'%modelidvar)
           row = curs.fetchall()
           if row :                 

                 liste1 = row[0][12].split(",")
                 print liste1
           print liste1


           i = 0
           while i < len(liste1) :
           #for i in range(len(liste1)):

                print tailles_entry[i].get()
                i = i + 1 
                 #qtetaillevar[i] = str(qtetaillevar[i])
                 #print "qtetaillevar[%s]"%i


    def valider():

           global distligne                                 

           qtetaillevar = {}

           distligne = 0

           modelidvar = modelid_entry.get()

           connect()
           curs = conn.cursor()                           
           curs.execute("UPDATE model SET operation='Lavage' WHERE modelid=%s"%modelidvar)
           curs.execute(' select client.*, model.* from client,model where client.clientid=model.client AND modelid=%s ;'%modelidvar)
           row = curs.fetchall()           

           if row :                 

                 liste1 = row[0][12].split(",")
                 print liste1
                 print liste1[0]              
                 print liste1[1]

                 print len(liste1)
                 #for i in range(len(liste1)):
                 i=0
                 while i < len(liste1) :
                     qtetaillevar[i] = StringVar() 
                     distligne = 30 + distligne                
                     label100 = Label(interf2 , text="Quantité/%s"%(liste1[i]) , justify=LEFT,  bg = "blue", fg = "white" ).place(x=30 , y=165+distligne)                               
                     tailles_entry[i] = ttk.Entry(interf2, width=13, textvariable=qtetaillevar[i]).place (x = 130 , y = 165+distligne)
                     i = i +1

                 lab2.config(text="Veuillez saisir les données du Model : %s"%row[0][4] ,font=("times",10,"bold"), bg = "blue")
                 lab2.place( x = 110 , y = 135 )
                 lab3.place ( x=15 , y=30)
                 lab31= Label(interf2, text="%s"%row[0][3] ,font=("times",10,"bold"), bg = "blue", fg = "red" )
                 lab31.place ( x=105 , y=30)
                 lab4= Label(interf2, text="Client :" , bg = "blue", fg = "white" )
                 lab4.place ( x=202 , y=30)
                 lab41= Label(interf2, text="%s"%row[0][1] ,font=("times",10,"bold"), bg = "blue", fg = "red" )
                 lab41.place ( x=238 , y=30)
                 lab5= Label(interf2, text="Atelier       :" , bg = "blue", fg = "white" )
                 lab5.place ( x=370 , y=30)
                 lab51= Label(interf2, text="%s"%row[0][2] ,font=("times",10,"bold"), bg = "blue", fg = "red" )
                 lab51.place ( x=429 , y=30)
                 lab6= Label(interf2, text="Matricule Model   :" , bg = "blue", fg = "white" )
                 lab6.place ( x=15 , y=60)
                 lab61= Label(interf2, text="%s"%row[0][4] ,font=("times",10,"bold"), bg = "blue", fg = "red" )
                 lab61.place ( x=105 , y=60)
                 lab7= Label(interf2, text="Opération :" , bg = "blue", fg = "white" )
                 lab7.place ( x=370 , y=60)
                 lab71= Label(interf2, text="%s"%row[0][10] ,font=("times",10,"bold"), bg = "blue", fg = "red" )
                 lab71.place ( x=429, y=60)
                 lab7= Label(interf2, text="Quantité demandée :" , bg = "blue", fg = "white" )
                 lab7.place ( x=202 , y=60)
                 lab71= Label(interf2, text="%s"%row[0][9] ,font=("times",10,"bold"), bg = "blue", fg = "red" )
                 lab71.place ( x=307 , y=60)

                 lab8= Label(interf2, text="Numero de BON :" , bg = "blue", fg = "white" )
                 lab8.place ( x=30 , y=165)                                 
                 numbon_entry = ttk.Entry(interf2, width=13, textvariable=numbonvar)
                 numbon_entry.place (x = 130 , y = 165)

                 valider.config(command = valider1)
                 valider.place(x=350 , y=200)
                 #valider.place_forget()
                 modelid_entry.place_forget()
                 print row[0][1]


    valider = Button(interf2, text = " Valider ", command = valider)
    valider.place( x=250, y=100)           

    revenir = Button(interf2, text = " revenir ", command = revenir)
    revenir.place( x=12, y=300)

    Quitter = Button(interf2, text = "Quitter" , command = interf2.destroy)
    Quitter.place( x=230, y=350 )

    interf2.after(0,center,interf2)

    interf2.mainloop()

After clicking on "valider1" button i got this error message :

OperationalError: (1054, "Unknown column 'PY_VAR4' in 'where clause'")

Recommended Answers

All 22 Replies

I think PY_VAR4 is the TCL name of your StringVar. Try to use modelidvar.get() at line 46. Also the % operator is not the prefered way to substitute values in SQL statements.

Hello ,
Thank you very much Gribouillis , when i used modelidvar.get() , it could print "liste1" at line 46 , but couldn't execute the instruction at line 59 , the error i got is :

TypeError: cannot concatenate 'str' and 'int' objects

Hello ,
Thank you very much Gribouillis , when i used modelidvar.get() , it could print "liste1" at line 46 ,
but couldn't execute the instruction at line 59 , the error i got is : TypeError: cannot concatenate 'str' and 'int' objects

Can you post the full traceback ? It seems unlikely that the error comes from line 59.

What I don't like in the code is your use of global variables everywhere. It is likely to generate cryptic errors. Write a class

class Glo:
    pass
glo = Glo()

then use glo.distligne , etc instead of global variables and remove all global statements.

Hello , thius is all the traceback :

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in call
return self.func(*args)
File "C:\Documents and Settings\Administrateur.JUKI-PC\Bureau\ProjetsPython\Private\LogisticProject\Depotdistant.py", line 183, in valider1
qtetaillevar[i] = tailles_entry[i].get()
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1206, in cget
return self.tk.call(self._w, 'cget', '-' + key)
TypeError: cannot concatenate 'str' and 'int' objects

Can you explain to me please the issue of global variables ? thank you very much

in qtetaillevar[i] = tailles_entry[i].get(), you could check that tailles_entry is a list. Your error message suggests that it is a tkinter type. Try

print(type(tailles_entry))
item = tailles_entry[i]
print(type(item))
qtetaillevar[i] = item.cget()

Hello again ,

i did try : 1.print(type(tailles_entry)) , it prints good : <type 'instance'>
but it stops at : 2.item = tailles_entry[i] , showing the same error message : TypeError: cannot concatenate 'str' and 'int' objects .

Hello!
There are inconsistencies in your variable's types. For exemple in the code above, at line 24, qtaillevar is a StringVar, but at line 26, it is a dictionary. In the same way tailles_entry is a ttk.Entry at line 36, but it is used elsewhere with a subscript [i] like a list. You should decide which is the type of each variable and check that every expression where this variable appears uses the variable according to this type.

Hello ,
Thank you for your replies Gribouillis , in reality i couldn't verify the types on that code , because i was trying wich way can deal out with that message error .
I did a new simplified code ( the function that creates entries ) , and it shows the same message error , even there is no much declaration of types of variables , here is the simplified code :

# -*- coding: cp1252 -*-
# -*- coding: cp1252 -*-
from Tkinter import *
import Tkinter as tk

import os
import ttk
import MySQLdb;



def valider() :
    while i < len(b) :
         qtetaillevar[i] =  tailles_entry[i].get()
         print qtetaillevar[i]
def Interface2() :


    interf2 = Tk()    
    interf2.geometry("500x400")
    interf2.title('                                Gestion du Depot ')  

    f1 = Frame(interf2, bg="blue",  width=500, height=700)
    f1.pack( fill=X, expand=0)

    lab2 = Label(interf2, text="Veuillez saisir le numero d'Import du Model ( num+année , ex: 122013 )\n et cliquer sur valider " , bg = "blue", fg = "white" )
    lab2.place ( x=80 , y=45 )

    tailles_entry = ttk.Entry(interf2, width=13)

    distligne = 0
    b = []
    b=["S","M","L"]
    qtetaillevar = b
    i=0
    def valider() :
        i=0
        for i in range(len(b)):
            print(type(tailles_entry))
            item = str(tailles_entry[i])
            print(type(item))
            #qtetaillevar[i] = item.cget()

    while i < len(b) :

        qtetaillevar[i] = ''
        distligne = 30 + distligne 
        tailles_entry[i] = ttk.Entry(interf2, width=13, textvariable=qtetaillevar[i]).place (x = 130 , y = 165+distligne)

        i = i+1


    valider = Button(interf2, text = " Valider ", command = valider)
    valider.place( x=250, y=100)           

    interf2.mainloop()



Interface2()        

Again, tailles_entry is a single ttk.Entry instance at line 29, then what does tailles_entry[i] mean ? You should probably create a list of entries.

Hello Gribouillis
I explain to you again what I want to have like result , i need to create a number of entries ( this number depend on the number of the element of the list "b" , here the number is fix = 3 ) , and i want to insert the texts entered into my database , actualy i just need to print them ( just print what is typped on the entries , the 3 ones here ) .
I did add : tailles_entry = ttk.Entry(interf2, width=13) , because without it i got this error message :

NameError: global name 'tailles_entry' is not defined

tailles_entry[i] means , for example if i = 0 , the first ttk entry created .
Now , i just need to be able to print what is wrotten in that 3 ttk entries .

Ok, if you want to create a number of entries, use a list and a loop

nbr_entries = len(b)
tailles_entry = [] # a list
for i in range(nbr_entries):
    entry = ttk.Entry(interf2, width=13)
    tailles_entry.append(entry) # add a new entry to the list

That's not working , or i couldn't fix it with your code , that don't create entries on the window .
And as I tald you i did already create entries , but couldn't use the data putted in these entries ...

Hello ,
I tried to modify the code to this one :

# -*- coding: cp1252 -*-
# -*- coding: cp1252 -*-
from Tkinter import *
import Tkinter as tk

import os
import ttk


def Interface2() :


    interf2 = Tk()    
    interf2.geometry("500x400")
    interf2.title('                                Gestion du Depot ')  

    f1 = Frame(interf2, bg="blue",  width=500, height=700)
    f1.pack( fill=X, expand=0)

    lab2 = Label(interf2, text="Veuillez saisir le numero d'Import du Model ( num+année , ex: 122013 )\n et cliquer sur valider " , bg = "blue", fg = "white" )
    lab2.place ( x=80 , y=45 )

    distligne = 0   

    b=['S','M','L']
    qtetaillevar = b
    i=0
    print qtetaillevar

    def valider() :

        for i in range(len(b)):
            print '%s'%qtetaillevar[i]
            #a[i] = str(qtetaillevar[i])
            qtetaillevar[i] = entry.get()
            print qtetaillevar[i]

    #def valider() :
       # i=0
        #for i in range(len(b)):
           # print(type(tailles_entry))
           # item = tailles_entry[i]
           # print(type(item))
           # qtetaillevar[i] = item.cget()

    tailles_entry = []
    for i in range(len(b)):
        distligne = 30 + distligne 
        entry = ttk.Entry(interf2, width=13 , textvariable=qtetaillevar[i]).place (x = 130 , y = 165+distligne)  
        tailles_entry.append(entry)

        ##qtetaillevar[i] = ''
        #distligne = 30 + distligne 
       # tailles_entry[i] = ttk.Entry(interf2, width=13, textvariable=qtetaillevar[i]).place (x = 130 , y = 165+distligne)        
       # i = i+1

    valider = Button(interf2, text = " Valider ", command = valider)
    valider.place( x=250, y=100)           

    interf2.mainloop()

Interface2()        

But after executing the code it shows an other message error :

Exception in Tkinter callback
Traceback (most recent call last):
File "E:\Python27\lib\lib-tk\Tkinter.py", line 1410, in call
return self.func(*args)
File "E:\Documents and Settings\Administrateur.METHODE1-PC\Bureau\Essaie_dynamicentries.py", line 38, in valider
qtetaillevar[i] = entry.get()
AttributeError: 'NoneType' object has no attribute 'get'

I just need to know how could i print the datas entered in the entries ...

Here is a first working version

#/usr/bin/env python
# -*-coding: utf8-*-
from Tkinter import *
import Tkinter as tk

import os
import ttk

b = ["S","M","L"]
qtetaillevar = b
tailles_entry = []

def valider():
    for i in range(len(b)):
        qtetaillevar[i] =  tailles_entry[i].get()
        print qtetaillevar[i]

def Interface2() :


    interf2 = Tk()    
    interf2.geometry("500x400")
    interf2.title('                                Gestion du Depot ')  

    f1 = Frame(interf2, bg="blue",  width=500, height=700)
    f1.pack( fill=X, expand=0)

    lab2 = Label(interf2, text="Veuillez saisir le numero d'Import du Model ( num+année , ex: 122013 )\n et cliquer sur valider " , bg = "blue", fg = "white" )
    lab2.place ( x=80 , y=45 )

    distligne = 0


    for i in range(len(b)):
        qtetaillevar[i] = StringVar()
        distligne = 30 + distligne 
        entry = ttk.Entry(interf2, width=13, textvariable=qtetaillevar[i])
        entry.place (x = 130 , y = 165+distligne)
        tailles_entry.append(entry)


    bou_valider = Button(interf2, text = " Valider ", command = valider)
    bou_valider.place( x=250, y=100)           

    interf2.mainloop()



Interface2()  

Hello again Gribouillis

I'm thanking you so much for fixing the issue i was searching for it since last week , good luck to you friend . I hope if we can stay in contact , good continuation .

Hello again Gribouillis ,
In reality the code i made is not good one , i did try to apply the example you gave to me into that code , but i'm just failling in the last step ; in the function "valider1" , at the line : qtetaillevar[i] = tailles_entry[i].get() , the message Error i have is :

AttributeError: 'NoneType' object has no attribute 'get' .

I show again all the code of this window :
`def Interface2() :

global interf2
interf2 = Tk()    
interf2.geometry("500x400")
interf2.title('                                Gestion du Depot ')  

f1 = Frame(interf2, bg="blue",  width=500, height=700)
f1.pack( fill=X, expand=0)

lab2 = Label(interf2, text="Veuillez saisir le numero d'Import du Model ( num+année , ex: 122013 )\n et cliquer sur valider " , bg = "blue", fg = "white" )
lab2.place ( x=80 , y=45 )

global liste1
global distligne
global modelidvar
global numbonvar

modelidvar = StringVar()
numbonvar = StringVar()

distligne = 0

lab3= Label(interf2, text="Référence Model :" , bg = "blue", fg = "white" )
lab3.place ( x=30 , y=100)
modelid_entry = ttk.Entry(interf2, width=13, textvariable=modelidvar)
modelid_entry.place (x = 140 , y = 100 )
numbon_entry = ttk.Entry(interf2, width=13, textvariable=numbonvar)

tailles_entry = []

def revenir():                      
       interf2.destroy()
       InterfaceDeps()      
def valider1():
       print "valider1"
       #print liste1
       numbonvar = numbon_entry.get()
       modelidvar = modelid_entry.get()
       connect()
       curso = conn.cursor()
       curso.execute(' select client.*, model.* from client,model where client.clientid=model.client AND modelid=%s ;'%modelidvar)
       row1 = curso.fetchall()
       if row1 :
          liste1 = row1[0][12].split(",")

       print liste1
       qtetaillevar = liste1
       for i in range(len(liste1)):

            qtetaillevar[i] = tailles_entry[i].get()
            print qtetaillevar[i]
             #qtetaillevar[i] = str(qtetaillevar[i])
             #print "qtetaillevar[%s]"%i


def valider():

       global distligne

       distligne = 0

       modelidvar = modelid_entry.get()

       connect()
       curs = conn.cursor()                           
       curs.execute(' update model set operation="Lavage" where modelid="%s" ;'%modelidvar)
       curs.execute(' select client.*, model.* from client,model where client.clientid=model.client AND modelid=%s ;'%modelidvar)
       row = curs.fetchall()           

       if row :                 

             liste1 = row[0][12].split(",")
             print liste1
             print liste1[0]              
             print liste1[1]
             qtetaillevar = liste1

             print len(liste1)
             #for i in range(len(liste1)):

             for i in range(len(liste1)) :

                 distligne = 30 + distligne                
                 label100 = Label(interf2 , text="Quantité/%s"%(liste1[i]) , justify=LEFT,  bg = "blue", fg = "white" ).place(x=30 , y=165+distligne)                               
                 qtetaillevar[i] = StringVar() 
                 entry = ttk.Entry(interf2, width=13, textvariable=qtetaillevar[i]).place (x = 130 , y = 165+distligne)
                 tailles_entry.append(entry)

             lab2.config(text="Veuillez saisir les données du Model : %s"%row[0][4] ,font=("times",10,"bold"), bg = "blue")
             lab2.place( x = 110 , y = 135 )
             lab3.place ( x=15 , y=30)
             lab31= Label(interf2, text="%s"%row[0][3] ,font=("times",10,"bold"), bg = "blue", fg = "red" )
             lab31.place ( x=105 , y=30)
             lab4= Label(interf2, text="Client :" , bg = "blue", fg = "white" )
             lab4.place ( x=202 , y=30)
             lab41= Label(interf2, text="%s"%row[0][1] ,font=("times",10,"bold"), bg = "blue", fg = "red" )
             lab41.place ( x=238 , y=30)
             lab5= Label(interf2, text="Atelier       :" , bg = "blue", fg = "white" )
             lab5.place ( x=370 , y=30)
             lab51= Label(interf2, text="%s"%row[0][2] ,font=("times",10,"bold"), bg = "blue", fg = "red" )
             lab51.place ( x=429 , y=30)
             lab6= Label(interf2, text="Matricule Model   :" , bg = "blue", fg = "white" )
             lab6.place ( x=15 , y=60)
             lab61= Label(interf2, text="%s"%row[0][4] ,font=("times",10,"bold"), bg = "blue", fg = "red" )
             lab61.place ( x=105 , y=60)
             lab7= Label(interf2, text="Opération :" , bg = "blue", fg = "white" )
             lab7.place ( x=370 , y=60)
             lab71= Label(interf2, text="%s"%row[0][10] ,font=("times",10,"bold"), bg = "blue", fg = "red" )
             lab71.place ( x=429, y=60)
             lab7= Label(interf2, text="Quantité demandée :" , bg = "blue", fg = "white" )
             lab7.place ( x=202 , y=60)
             lab71= Label(interf2, text="%s"%row[0][9] ,font=("times",10,"bold"), bg = "blue", fg = "red" )
             lab71.place ( x=307 , y=60)

             lab8= Label(interf2, text="Numero de BON :" , bg = "blue", fg = "white" )
             lab8.place ( x=30 , y=165)                                 

             numbon_entry.place (x = 130 , y = 165)

             valider.config(command = valider1)
             valider.place(x=350 , y=200)
             #valider.place_forget()
             modelid_entry.place_forget()
             print row[0][1]


valider = Button(interf2, text = " Valider ", command = valider)
valider.place( x=250, y=100)           

revenir = Button(interf2, text = " revenir ", command = revenir)
revenir.place( x=12, y=300)

Quitter = Button(interf2, text = "Quitter" , command = interf2.destroy)
Quitter.place( x=230, y=350 )

interf2.after(0,center,interf2)

interf2.mainloop()`

AttributeError: 'NoneType' object has no attribute 'get' .

I think the place() method returns the None object, so that one must not write

entry = ttk.Entry(...).place(...)
tailles_entry.append(entry)

but

entry = ttk.Entry(...)
entry.place(...)
tailles_entry.append(entry)

Hello Gribouillis ,
Thank you very much again for your replies , i will try , and tell you the result .

Hello ,
That worked good as you said , thank you so much Gribouillis , I will ask you when i'll face an other issue , good luck to you .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.