myEntry

Il widget Entry serve per permettere all’ utente di passare in ingresso un valore che può essere successivamente convertito in qualsiasi tipo. Esempio: stringa, numero intero, numero float, ecc.

myEntry

Interessante notare l’ attributo numb che ci permette di decidere il numero di caratteri che saranno visibili nella nostra Entry.

# myEntry
#-----------------------------------------------------------------------------
def myEntry(name='myText', 
			numb=None, 
			call=None, data=['dati']):
	""" crea una entry text di una certa dimensione 'numb'
		con un testo di default 'text'
		
		-> name testo da inserire nella entry
		-> numb numero massimo di caratteri visibili 
		-> call funzione da eseguire su evento
		-> data dati da passare alla funzione
	"""
	#callback debug    
	def on_activate(widg, *data):
		print "a", widg.get_text()
#entry
	# istanzio una Entry
	entr = Gtk.Entry()
	entr.show()
	# referenzio l'istanza
	entr.iden = str(entr.weak_ref)[-11:-1]
	# imposto massima lunghezza visibile in caratteri
	if numb == None:
		entr.set_width_chars(len(name))
	else:
		# max visible characters
		entr.set_width_chars(numb)
		# max entry digit
		#entr.set_max_length(numb)
		#entr.set_sensitive_length(False)
	entr.set_text(str(name))
	
	# in assenza di callback usa quella di debug
	if call == None:
		call = on_activate
	entr.connect("activate", call, *data)	
# <-        
	return entr, call
#-----------------------------------------------------------------------------
def testEntry():
#myEntry
	# ridefinisco la callback        
	def on_activate(widg, *data):
		print "b", widg.get_text(), data
	# entr, call
	entr, call = myEntry(name='myEntry',
						 numb=None, 
						 call=on_activate, data=["i miei dati",])
# <-
	return entr

L’evento usato in questo caso si chiama on_activate e ce ne serviamo per raccogliere i dati scritti quando l’ utente preme il CR (Carrige Return).

	#callback debug    
	def on_activate(widg, *data):

Da notare che possiamo passare dei dati che possono essere elaborati dalla nostra callback. Durante il test viene fatto un esempio. Inoltre viene ridefinita la callback per dimostrare la grande flessibilità di questo modo di operare.

	# ridefinisco la callback        
	def on_activate(widg, *data):
		print "b", widg.get_text(), data

Se proviamo ad avviare il test otterremo quanto segue.

alternate text

testEntry in esecuzione. Guardare il risultato sul terminale da cui si è avviato lo script.

myEntList

Come sempre aggiungiamo la nostra lista che ci sarà utile più avanti.

#-----------------------------------------------------------------------------
# myEntryList
#-----------------------------------------------------------------------------
def myEntList(name=["One","Two","Three"], 
			  numb=None, 
			  call=None, data=['dati'],
			  tBox='v', aBox=[False, False, 1]):
	#callback debug
	def on_activate(widg, ind, *data):
		print "a", widg
		print ind, data
		
	# in assenza di callback uso quella di debug
	if call == None:
		call = on_activate

	# funzione che istanzia oggetti tipo
	def myList(ind):
#myEntry
		# entr, call
		return myEntry(name[ind], numb, 
					   call, [ind, data])
#myBoxList
	# xBox, [entr, call] * N
	obje, othe = myBoxList(name=name, tBox=tBox, 
						   aBox=aBox, func=myList)
# <-
	return obje, othe
#-----------------------------------------------------------------------------
def testEntList():
#myEntList    
	# ridefinisco la callback
	def on_activate(widg, ind, *data):
		pass
		print "b", "%05s" %widg.get_text(), ind, data
	# xBox, [entr, call] * N
	obje, othe = myEntList(name=["One","Two","Three"], 
						   numb=6, 
						   call=on_activate, data=[],
						   tBox='h', aBox=[False, False, 1] )
#myFrame    
	# fram,[labe,xBox]
	obj1, oth1 = myFrame(name='myEntry', obje=obje, colo="black",
						 bord=2, shad=Gtk.SHADOW_ETCHED_OUT,
						 tBox='v' )
	#debug
	myViewObject(obje, othe)
# <-
	return obj1

Unica cosa da notare nella callback delle entry list abbiamo un parametro in più ind che ci permetterà di risalire a quale oggetto appartiene l’ evento generato.

	#callback debug
	def on_activate(widg, ind, *data):
		print "a", widg
		print ind, data

Se proviamo ad avviare il test otterremo quanto segue.

alternate text

testEntList in esecuzione. Se guardate il risultato nel solito terminale vedrete che se provate a generare i 3 eventi sulle 3 entry vi restituiscono l ‘indice per ogni evento che vi sarà utile a risalire a quale entry appartengono i dati restituiti.

alternate text

uscita dal terminale.

myEntLabel

Per dare una identificazione ad ogni singola entry possiamo aggiungere una label al suo fianco

#-----------------------------------------------------------------------------
# myEntryLabel
#-----------------------------------------------------------------------------
def myEntLabel(name='myText', 
			   numb=None,
			   call=None, data=['dati'],
			   nLab='Label', cLab="#333",
			   tLab='h', aLab=[False, False, 1]):
	""" crea una entry text di una certa dimensione 'numb'
		con un testo di default 'text' con associata una
		label alla riga o colonna
		  
		-> name testo da inserire nella entry
		-> numb numero massimo di caratteri visibili 
		-> call funzione da eseguire su evento
		-> data dati da passare alla funzione
		-> nLab nome etichetta 
		-> cLab colore etichetta 
		-> tLab tipo contenitore etichetta v/h 
		-> aLab attributi contenitore etichetta 
	"""
#myEntry
	# entr, call
	entr,call = myEntry(name=name,
			  			numb=numb, 
			  			call=call, data=data)
#myLabel
	if cLab == None:
		cLab=Gdk.color_parse('blue')
	# labe
	labe = myLabel(name=nLab, 
					leng=len(nLab)+1, prea=' ', post=' ', 
					font='Courier 10', 
					colo=cLab)
#xBox   
	xBox = myBox(tLab)
	#child, expand=True, fill=True, padding=0
	xBox.pack_start(labe, *aLab)
	xBox.pack_start(entr, *aLab)	
# <-        
	return xBox, [entr, call, labe]
#-----------------------------------------------------------------------------
def testEntLabel():
#myEntLabel
	# ridefinisco la callback        
	def on_activate(widg, *data):
		print "b", widg.get_text(), data
	# xBox, [entr, call, labe]
	obje, othe = myEntLabel(name='myText', 
							numb=None,
							call=on_activate, data=[],
							nLab='Label', cLab="#333",
							tLab='h', aLab=[False, False, 1])
# <-
	return obje

Il codice è molto simile a myEntry con l’ aggiunta degli attributi della label aggiuntiva.

Se proviamo ad avviare il test otterremo quanto segue.

alternate text

testEntLable in esecuzione.

myEntLabList

Sarò ripetitivo ma aggiungiamo la nostra solita lista.

#-----------------------------------------------------------------------------
# myEntryLabelList
#-----------------------------------------------------------------------------
def myEntLabList(name=["One","Two","Three"], 
				 numb=None, 
				 call=None, data=['dati'],
				 nLab=['Label01','Label02','Label03'], cLab="#333",
				 tLab='h', aLab=[False, False, 1],
				 tBox='h', aBox=[False, False, 1],):
	#callback debug
	def on_activate(widg, ind, *data):
	  print "a", widg
	  print ind, data
		
	# in assenza di callback uso quella di debug
	if call == None:
	  call = on_activate

	# funzione che istanzia oggetti tipo
	def myList(ind):
#myEntLabel
		# xBox, [entr, call, labe]
		return myEntLabel(name[ind], numb,
						  call, [ind, data],
						  nLab[ind], cLab,
						  tLab, aLab)
#myBoxList
	# xBox, [xBox, [entr, call, labe]] * N
	obje, othe = myBoxList(name=name, tBox=tBox, 
						   aBox=aBox, func=myList)
# <-
	return obje, othe
#-----------------------------------------------------------------------------
def testEntLabList():
#myEntLabList    
	# ridefinisco la callback
	def on_activate(widg, ind, *data):
		print "b", "%05s" %widg.get_text(), ind, data
	# xBox, [xBox, [entr, call, labe]] * N
	obje, othe = myEntLabList(name=["One","Two","Three"], 
							  numb=6, 
							  call=on_activate, data=[],
							  nLab=['Label01','Label02','Label03'], cLab="#333",
							  tLab='h', aLab=[False, False, 1],
							  tBox='v', aBox=[False, False, 1],)
#myFrame    
	# fram,[labe,xBox]
	obj1, oth1 = myFrame(name='myEntLabList', obje=obje, colo='black',
						 bord=2, shad=Gtk.SHADOW_ETCHED_OUT,
						 tBox='v' )
	#debug
	myViewObject(obje, othe)
# <-
	return obj1

Se proviamo ad avviare il test otterremo quanto segue.

alternate text

testEntLabList in esecuzione.

myEntFrame

Per dare una identificazione ad ogni singola entry possiamo anche ricorrere ad un frame.

#-----------------------------------------------------------------------------
# myEntryFrame
#-----------------------------------------------------------------------------
def myEntFrame(name='myText', 
			   numb=None,
			   call=None, data=['dati'],
			   nFra='Label', cFra='black', bFra=1, sFra=Gtk.SHADOW_ETCHED_OUT, 
			   tFra='v', aFra=[False, False, 1]):
	""" crea una entry text di una certa dimensione 'numb'
		con un testo di default 'text' inserita in un frame
		
		-> name testo da inserire nella entry
		-> call funzione da eseguire su evento
		-> data dati da passare alla funzione
		-> numb numero massimo di caratteri visibili 
		-> nFra nome del frame 
		-> cFra colore nome del frame 
		-> bFra bordo riservato all'esterno
		-> sFra tipo di cornice
		-> tFra tipo di contenitore v/h 
		-> aFra attributi del contenitore
	"""    
#myEntry
	entr,call = myEntry(name=name,
						numb=numb,
						call=call, data=data)
#myFrame
	#fram, [labe, xBox]
	fram,othe = myFrame(nFra, entr, cFra, bFra, sFra, tFra, aFra)
	# <-
	#fram, [labe, xBox, entr, call]        
	return fram, [othe[0], othe[1], entr, call]
#-----------------------------------------------------------------------------
def testEntFrame():
#myEntFrame
	# ridefinisco la callback        
	def on_activate(widg, *data):
		pass
		print "b", data
	#fram, [labe, xBox, entr, call]        
	obje, othe = myEntFrame(name='myText', 
							numb=6,
							call=on_activate, data=[],
							nFra='myEntFrame', cFra='black', bFra=1, sFra=Gtk.SHADOW_ETCHED_OUT, 
							tFra='v', aFra=[False, False, 1])
# <-
	return obje

Il codice cambia solo per gli attributi associati al frame.

Se proviamo ad avviare il test otterremo quanto segue.

alternate text

testEntFrame in esecuzione.

myEntFraList

Per dare una identificazione ad ogni singola entry possiamo anche ricorrere ad un frame.

#-----------------------------------------------------------------------------
# myEntryFramelList
#-----------------------------------------------------------------------------
def myEntFraList(name=["One","Two","Three"], 
				 numb=None, 
				 call=None, data=['dati'],
				 nFra=['Label01','Label02','Label03'], 
				 cFra='black',
				 bFra=1, sFra=Gtk.SHADOW_ETCHED_OUT, 
				 tFra='v', aFra=[False, False, 1],
				 tBox='h', aBox=[False, False, 1]):
	#callback debug
	def on_activate(widg, ind, *data):
		print "a", 
		print ind, data
		
	# in assenza di callback uso quella di debug
	if call == None:
		call = on_activate

	# funzione che istanzia oggetti tipo
	def myList(ind):
#myEntFram
		#fram, [labe, xBox, entr, call]
		return myEntFrame(name[ind], numb,
						  call, [ind, data],
						  nFra[ind], cFra, bFra, sFra, 
						  tFra, aFra)
#myBoxList
	# xBox, [fram, [labe, xBox, entr, call]] * N        
	obje, othe = myBoxList(name=name, tBox=tBox, 
						   aBox=aBox, func=myList)
# <-
	return obje, othe
#-----------------------------------------------------------------------------
def testEntFraList(debu=0):
#myEntFraList    
	# ridefinisco la callback
	def on_activate(widg, ind, *data):
		print "b", "%05s" %widg.get_text(), ind, data
	# xBox, [fram, [labe, xBox, entr, call]] * N        
	obje, othe = myEntFraList(name=["One","Two","Three"], 
							  numb=None, 
							  call=on_activate, data=[],
							  nFra=['Label01','Label02','Label03'],
							  cFra='black', 
							  bFra=1, sFra=Gtk.SHADOW_ETCHED_OUT, 
							  tFra='v', aFra=[False, False, 1],
							  tBox='h', aBox=[False, False, 1])
#myFrame    
	# fram,[labe,xBox]
	obj1, oth1 = myFrame(name='myEntryFrame', obje=obje, colo='black',
						 bord=2, shad=Gtk.SHADOW_ETCHED_OUT,
						 tBox='v' )
	#debug
	myViewObject(obje, othe)
# <-
	return obj1

Se proviamo ad avviare il test otterremo quanto segue.

alternate text

testEntFraList in esecuzione.

Package

La struttura aggiornata del nostro package è la seguente:

l00_start.py
l01_startGtk.py
my00init.py
myWidg/
  __init__.py
  my00init.py
  my00initGtk.py
  my01Box.py
  my02Label.py
  my02Entry.py
  myWind.py
  myApp.py

Per scaricare la nuova versione 20150828.zip

Saluti

Bene anche questo post è finito. Spero di esservi stato utile. Se c’ è qualcosa che non va non esitate a segnalarlo.

Nel prossimo post vedremo gli oggetti txtView.

Ciao alla prossima. (stay tune!)