[pygtk] How to render a SpinButton in a TreeView?

Joel Juvenal Rivera Rivera joelriv at gmail.com
Mon Dec 21 12:49:16 WST 2009


Thanks for your answer,

I didn't know about that bug,
and actually my question was,how to draw the widget
i mean static, like a pixbuf, but after reading your code,
and some other doc about TreeViews i got the idea
it get draws like text, but when you click, it appears
the spin button.

Regarts, Joel Rivera

     


On Sun, 2009-12-20 at 15:17 +0100, Jiri Bajer wrote: 
> On Sun, 2009-11-01 at 06:26 +0100, Joel Juvenal Rivera Rivera wrote:
> > I hope some one coud help me, i can't draw a spin button in a TreeView, 
> > im using CellRendererSpin but how do i draw "the widget" not just the
> > text o something like that (in the CellRendererText for example).
> 
> I used CellRendererSpin the following way - hope it helps a bit:
> 
> class CellRendererSpinFixed(gtk.CellRendererSpin):
> 	# the whole class is just a workaround for PyGTK bug 492196
> 
> 	__gtype_name__ = 'CellRendererSpinFixed'
> 
> 	def __init__(self):
> 		gtk.CellRendererSpin.__init__(self)
> 
> 	def do_start_editing(self, event, treeview, path, background_area,
> cell_area, flags):
> 		if not self.get_property('editable'):
> 			return
> 		spin = gtk.SpinButton(self.get_property('adjustment'))
> 		spin.connect('editing-done', self._editing_done, path)
> 		spin.connect('key-press-event', self._key_press_event, path)
> 		spin.connect('button-press-event', self._button_press_event)
> 		spin.show()
> 		spin.grab_focus()
> 		return spin
> 
> 	def _editing_done(self, spin, path):
> 		self.emit('edited', path, spin.get_property('value'))
> 
> 	def _key_press_event(self, spin, event, path):
> 		if event.type == gtk.gdk.KEY_PRESS:
> 			if gtk.gdk.keyval_name(event.keyval) == 'Up':
> 				spin.spin(gtk.SPIN_STEP_FORWARD)
> 				return True
> 			if gtk.gdk.keyval_name(event.keyval) == 'Down':
> 				spin.spin(gtk.SPIN_STEP_BACKWARD)
> 				return True
> 
> 	def _button_press_event(self, spin, event):
> 		# avoid unwanted 'editing done' if the user clicks on spin arrows too
> fast, see PyGTK bug 492196 for details
> 		if event.type == gtk.gdk._2BUTTON_PRESS or event.type ==
> gtk.gdk._3BUTTON_PRESS:
> 			return True # we have already received all the clicks, throw away
> additional signals telling it's double/tripleclick
> 
> ---
> 
> 		if column_type == 'spin':
> 			adjustment = gtk.Adjustment(lower=args['lower'], upper=args['upper'],
> step_incr=1, page_incr=10)
> 			renderer = CellRendererSpinFixed() # replace with regular
> cellrendererspin when bug 492196 gets finally fixed
> 			renderer.set_property('adjustment', adjustment)
> 			renderer.set_property('editable', True)
> 			renderer.set_property('digits', args['digits'])
> 			renderer.connect('edited', self._spin_update_model,
> len(self._model_columns))
> 			column = gtk.TreeViewColumn(title, renderer,
> text=len(self._model_columns))
> 			column.set_property('expand', False)
> 			column.set_property('reorderable', True)
> 			column.set_property('resizable', True)
> 			if args['digits']:
> 				self._model_columns.append(gobject.TYPE_FLOAT)
> 			else:
> 				self._model_columns.append(gobject.TYPE_INT)
> 			self._view_columns.append(column)
> 			return
> ---
> 
> 	def _spin_update_model(self, renderer, path, new_text, column):
> 		"""
> 			Updates the model whenever the spin value changes.
> 		"""
> 		digits = renderer.get_property('digits')
> 		if digits > 0:
> 			try:
> 				new_value = float(new_text)
> 			except:
> 				return
> 			(integral, decimal) = new_text.split('.')
> 			new_text = integral + '.' + decimal[0:digits]
> 			new_value = float(new_text)
> 		else:
> 			try:
> 				new_value = int(float(new_text)) # direct conversion of float-string
> to int raises an error
> 			except:
> 				return
> 
> self.model[self.sortable_model.convert_path_to_child_path(path)][column]
> = new_value
> 
> ---
> 
> 
> 




More information about the pygtk mailing list