myButton
Il modulo my03Button contiene una serie di widgets per la gestione del button tradizione.
myButton
Come nella entry passiamo oltre icon che definisce il tipo di icona che vogliamo usare la callback call per definire la funzione che vogliamo usare. Questa viene richiamata quando l’utente prema il pulsante. Il nome dell’ evento è il solito clicked.
#-----------------------------------------------------------------------------
# myButton
#-----------------------------------------------------------------------------
def myButton(name='my_Button',
icon=Gtk.STOCK_OK,
call=None, data=['dati']):
""" crea un bottone con associato una icona e una etichetta
alla premuta del bottone viene eseguita la callback associata
-> name nome associato alla label
-> icon tipo di icona associata
-> call funzione da eseguire su evento
-> data dati da passare alla funzione
"""
#callback debug
def on_clicked(widg, *data):
print "a", widg.props.label, data
#button
# istanzio un bottone
butt = Gtk.Button(stock=icon)
# lo rendo visibile
butt.show()
# confermo l'immagine attuale
butt.props.image = butt.get_image()
# assegno il nome alla label del bottone
butt.props.label = name
# in assenza di callback usa quella di debug
if call == None:
call = on_clicked
butt.connect('clicked', call, *data)
# <-
return butt, call
testButton
Come possiamo vedere dal codice del test se vogliamo visualizzare l’ icona, che di default rimane nascosta, dobbiamo abilitarla tramite apposita istruzione.
#-----------------------------------------------------------------------------
def testButton():
#myButton
# ridefinisco la callback
def on_clicked(widg, *data):
print "b", widg.props.label, data
# butt, call
obje, othe = myButton(name='my_Button',
icon=Gtk.STOCK_YES,
call=on_clicked, data=[])
# abilito la vista dell'icona che di default è nascosta
obje.set_always_show_image (True)
# <-
return obje
Se proviamo ad avviare il test otterremo quanto segue.

testButton in esecuzione.
myButList
Come al solito segue il metodo delle liste che ci torna utile nel caso dobbiamo definire una serie di pulsanti omogenei.
#-----------------------------------------------------------------------------
# myButton List
#-----------------------------------------------------------------------------
def myButList(name=["_Read","_Write","_Defau"],
icon=Gtk.STOCK_NO,
call=None, data=['dati'],
tBox='v', aBox=[False, False, 1]):
#callback debug
def on_clicked(widg, ind, *data):
print "a", ind, data
# in assenza di callback uso quella di debug
if call == None:
call = on_clicked
# funzione che istanzia oggetti tipo
def myList(ind):
#myButton
# butt,call
return myButton(name[ind], icon,
call, [ind, data])
#myBoxList
# xBox, [butt,call] * N
obje, othe = myBoxList(name=name, tBox=tBox,
aBox=aBox, func=myList)
# <-
return obje, othe
testButList
Anche qui ho aggiunto del codice di esempio. In questo caso vediamo come poter cambiare l’ icona già assegnata di default.
#-----------------------------------------------------------------------------
def testButList():
#myButList
# ridefinisco la callback
def on_clicked(widg, ind, *data):
print "b", ind, data
# xBox, [butt,call] * N
obje, othe = myButList(name=["_Read","_Write","_Default"],
icon=Gtk.STOCK_NO,
call=on_clicked, data=[],
tBox='v', aBox=[False, False, 1])
# abilito la vista dell'icona che di default è nascosta
othe[0][0].set_always_show_image (True)
othe[1][0].set_always_show_image (True)
othe[2][0].set_always_show_image (True)
# cambio icona ad alcuni bottoni
othe[0][0].props.image = Gtk.Image.new_from_stock(Gtk.STOCK_YES, Gtk.ICON_SIZE_BUTTON)
othe[1][0].props.image = Gtk.Image.new_from_stock(Gtk.STOCK_STOP, Gtk.ICON_SIZE_BUTTON)
#myFrame
# fram,[labe,xBox]
obj1, oth1 = myFrame(name='myButton', obje=obje, colo='black',
bord=2, shad=Gtk.SHADOW_ETCHED_OUT,
tBox='v' )
#debug
myViewObject(obje, othe)
# <-
return obj1
La lista delle icone predefinite la potete trovare al seguente link: icons
Se proviamo ad avviare il test otterremo quanto segue.

testButList in esecuzione.
myButFrame
A volte è utile enfatizzare l’ area dove si trova un pulsante. Per questo usiamo il frame.
#-----------------------------------------------------------------------------
# myButFrame
#-----------------------------------------------------------------------------
def myButFrame(name='my_Button',
nBut='myButton',
icon=Gtk.STOCK_OK,
call=None, data=['dati'],
bFra=1, sFra=Gtk.SHADOW_ETCHED_OUT,
tFra='v', aFra=[False, False, 1]):
""" crea un bottone con associato una icona in un frame con etichetta
alla premuta del bottone viene eseguita la funzione associata
-> name nome associato al frame label
-> nBut nome associato al button label
-> icon tipo di icona associata
-> call funzione da eseguire su evento
-> data dati da passare alla funzione
-> bFra bordo riservato all'esterno
-> sFra tipo di cornice
-> tFra tipo di contenitore v/h
-> aFra attributi del contenitore
"""
#callback debug
def on_clicked(widg, *data):
print "a", data
#button
# istanzio un bottone
butt = Gtk.Button(stock=icon)
# lo rendo visibile
butt.show()
# confermo l'immagine attuale
butt.props.image = butt.get_image()
# assegno il nome alla label del bottone
butt.props.label = nBut
#myFrame
#fram, [labe, xBox]
obje, othe = myFrame(name, butt, 'black', bFra, sFra, tFra, aFra)
# in assenza di callback usa quella di debug
if call == None:
call = on_clicked
butt.connect('clicked', call, *data)
# <-
#fram, [labe, xBox, butt, call]
return obje, [othe[0], othe[1], butt, call]
testButFrame
#-----------------------------------------------------------------------------
def testButFrame():
#myButFrame
# ridefinisco la callback
def on_clicked(widg, *data):
print "b", data
# fram, [labe, xBox, butt, call]
obje, othe = myButFrame(name='myButFrame',
nBut='myButton',
icon=Gtk.STOCK_OK,
call=on_clicked, data=[],
bFra=1, sFra=Gtk.SHADOW_ETCHED_OUT,
tFra='v', aFra=[False, False, 1])
# <-
return obje
Se proviamo ad avviare il test notiamo i 2 nomi diversi assegnati.

testButFrame in esecuzione.
myButFraList
La solita lista di oggetti.
#-----------------------------------------------------------------------------
# myButFraList
#-----------------------------------------------------------------------------
def myButFraList(name=["Read","Write","Default"],
nBut=["","",""],
icon=Gtk.STOCK_YES,
call=None, data=['dati'],
bFra=1, sFra=Gtk.SHADOW_ETCHED_OUT,
tFra='v', aFra=[False, False, 1],
tBox='h', aBox=[False, False, 1]):
#callback debug
def on_clicked(widg, ind, *data):
print "a", ind, data
# in assenza di callback uso quella di debug
if call == None:
call = on_clicked
# funzione che istanzia oggetti tipo
def myList(ind):
#myButFrame
# fram, [labe, xBox, butt, call]
return myButFrame(name[ind], nBut[ind], icon,
call, [ind, data],
bFra, sFra, tFra, aFra)
#myBoxList
# xBox, [fram, [labe, xBox, butt, call]] * N
obje, othe = myBoxList(name=name, tBox=tBox,
aBox=aBox, func=myList)
# <-
return obje, othe
testButFraList
Il codice di esempio.
#-----------------------------------------------------------------------------
def testButFraList():
#myButFraList
# ridefinisco la callback
def on_clicked(widg, ind, *data):
print "b", ind, data
# xBox, [fram, [labe, xBox, butt, call]] * N
obje, othe = myButFraList(name=["Read","Write","Default"],
nBut=["","",""],
icon=Gtk.STOCK_NO,
call=on_clicked, data=[],
bFra=1, sFra=Gtk.SHADOW_ETCHED_OUT,
tFra='v', aFra=[False, False, 1],
tBox='h', aBox=[False, False, 1])
# abilito la vista dell'icona che di default è nascosta
othe[0][1][2].set_always_show_image (True)
othe[1][1][2].set_always_show_image (True)
othe[2][1][2].set_always_show_image (True)
# cambio icona ad alcuni bottoni
othe[0][1][2].props.image = Gtk.Image.new_from_stock(Gtk.STOCK_YES, Gtk.ICON_SIZE_BUTTON)
othe[1][1][2].props.image = Gtk.Image.new_from_stock(Gtk.STOCK_YES, Gtk.ICON_SIZE_BUTTON)
othe[2][1][2].props.image = Gtk.Image.new_from_stock(Gtk.STOCK_YES, Gtk.ICON_SIZE_BUTTON)
#debug
myViewObject(obje, othe)
# <-
return obje
Se proviamo ad avviare il test otterremo quanto segue.

testButFraList 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
my02TxtView.py
my03Button.py
myWind.py
myApp.py
Per scaricare la nuova versione 20150901.zip
Saluti
Per oggi mi fermo qui.
Nel prossimo post vedremo alcune varianti del button.
Ciao alla prossima. (stay tune!)