How to manage absolute coordinate window in gtk3 python -
i translated treeview gtk2 tooltip in gtk3 it's running have problem manage coordinate of tooltip window.
i tooltip window locate near mouse et follow near. instead of project tooltip window located in corner of screen
my code
#!/usr/bin/env python # coding: utf-8 ''' warning data not update !!!!!!!!!!!!! treeviewtooltips.py provides treeviewtooltips, class presents tooltips cells, columns , rows in gtk.treeview. ------------------------------------------------------------ file includes demo. execute file: python treeviewtooltips.py ------------------------------------------------------------ use, first subclass treeviewtooltips , implement get_tooltip() method; see below. add number of gtk.treevew widgets treeviewtooltips instance calling add_view() method. overview of steps: # 1. subclass treeviewtooltips class mytooltips(treeviewtooltips): # 2. overriding get_tooltip() def get_tooltip(...): ... # 3. create instance mytips = mytooltips() # 4. build gtk.treeview. myview = gtk.treeview() ...# create columns, set model, etc. # 5. add view tooltips mytips.add_view(myview) how works: add_view() method connects treeview "motion-notify" event callback set private method. whenever mouse moves across treeview callback call get_tooltip() following arguments: get_tooltip(view, column, path) where, view: gtk.treeview instance. column: gtk.treeviewcolumn instance mouse over. path: path row mouse over. based on whether or not column , path checked specific values, get_tooltip can return tooltips cell, column, row or whole view: column checked path checked tooltip for... y y cell y n column n y row n n view get_tooltip() should return none if no tooltip should displayed. otherwise return value coerced string (with str() builtin) , stripped; if non-empty, result displayed tooltip. default, tooltip popup window displayed centered , below pointer , remain shown until pointer leaves cell (or column, or row, or view, depending on how get_tooltip() implemented). ''' gi.repository import gtk class treeviewtooltips_gtk3: """tooltip atttach treeview in gtk3 window moving follow row of treeview""" def __init__(self): self.win = gtk.window() self.win.set_decorated(false) self.win.set_default_size(-1, -1) self.label = gtk.label() self.win.add(self.label) # default, tooltip enabled self.__enabled = true def __show(self, tooltip, x, y): """in order move tooltip near row of treeview""" self.label.set_markup(tooltip) w,h = self.win.get_preferred_width() # move window self.win.move(*self.location(x,y,w,h)) # show self.win.show_all() return self.win def __leave_handler(self, view, event): """when pointer leaves view, hide tooltip""" self.win.hide() def enable(self): 'enable tooltip' self.__enabled = true def disable(self): 'disable tooltip' self.__enabled = false def location(self, x, y, w, h): '''given x,y coordinates of pointer , width , height (w,h) demensions of tooltip window, return x, y coordinates of tooltip window. default location center window on pointer , 4 pixels below it. # en clair c'est un décalage de position de la fenetre qui s'ouvre qui donne l'info ''' return x - w/2, y + 4 def tooltip_callback(self,treeview, x, y, keyboard_mode, tooltip): """ in order collect miscellaneous elemnt build data row of treeview""" """3eme règle implementer ça pour afficher un tooltip customisé en fonction des data du tree view""" x, y = treeview.convert_widget_to_bin_window_coords(x, y) if not self.__enabled : """if don't want tooltip display return false !!!!""" return false try: path, column, cell_x, cell_y = treeview.get_path_at_pos(x, y) iter = model.get_iter(path) #print 'tooltip=',self.get_tooltip(column, self.cust_col,path) treeview.set_tooltip_window(self.__show(self.get_tooltip(column, self.cust_col,path),x,y)) # necessary return true in order display window tooltip print "path valid :-)" return true except: # prevent case when path it's not valid print "perhaps path it's not valid ????" # not possible display return false def add_view(self, view): """add gtk.treeview tooltip check if view instance of gtk.treeview , build connector , tooltip enable""" assert isinstance(view, gtk.treeview), \ ('this handler should connected ' 'instances of gtk.treeview') # first condition in gtk3 # set true property "has-tooltip" view.set_property("has-tooltip", true) # second condition in gtk3 view.connect('query-tooltip',self.tooltip_callback) # hide tooltip when mouse out of widget view.connect("leave-notify-event", self.__leave_handler) def get_tooltip(self, view, column, path): """in order secure customized implementation in projet""" 'see module doc string description of method' raise notimplemented, 'subclass must implement get_tooltip()' if __name__ == '__main__': class demotips_gtk3(treeviewtooltips_gtk3): """demo play how """ def __init__(self, customer_column): """ init class customer_column instance of gtk.treeviewcolumn , being used in gtk.treeview show customer names.""" self.cust_col = customer_column # call base class init treeviewtooltips_gtk3.__init__(self) def get_tooltip(self, column,cust_col,path): """by checking both column , path have cell-based tooltip""" model = view.get_model() customer = model[path][2] if column cust_col: """here there lot of information on row""" return '<big>%s %s</big>\n<i>%s</i>' % (customer.fname, customer.lname, customer.notes) else: """here basic data !!!!""" return ('<big><u>generic column tooltip</u></big>\n' 'unless otherwise noted, all\n phone number %s ') % customer.phone # here's our customer class customer: def __init__(self, fname, lname, phone, notes): self.fname = fname self.lname = lname self.phone = phone self.notes = notes # create bunch of customers customers = [] fname, lname, phone, notes in [ ('joe', 'schmoe', '555-1212', 'likes morris dance.'), ('jane', 'doe', '555-2323', 'wonders hell\nmorris dancing is.'), ('phred', 'phantastic', '900-555-1212', 'dreams of betty.'), ('betty', 'boop', '555-3434', 'dreams in b&w.'), ('red sox', 'fan', '555-4545', "still livin' 2004!\nespecially after 2006.")]: customers.append(customer(fname, lname, phone, notes)) # build our model , view model = gtk.liststore(str, str, object) c in customers: model.append(['%s %s' % (c.fname, c.lname), c.phone, c]) view = gtk.treeview(model) # 2 columns, name , phone cell = gtk.cellrenderertext() cell.set_property('xpad', 20) namecol = gtk.treeviewcolumn('customer name', cell, text=0) namecol.set_min_width(200) view.append_column(namecol) cell = gtk.cellrenderertext() phonecol = gtk.treeviewcolumn('phone', cell, text=1) view.append_column(phonecol) # finally, connect tooltip, specifying name column # column want tooltip popup over. tips = demotips_gtk3(namecol) tips.add_view(view) # we're going demonstrate enable/disable. first need # callback function connect toggled signal. def toggle(button): if button.get_active(): tips.disable() else: tips.enable() # create checkbutton , connect our handler check = gtk.checkbutton('check disable view tooltips') check.connect('toggled', toggle) check.set_tooltip_markup('this standard gtk tooltip.\n' 'compare me tooltips above.') # create vbox pack view , checkbutton vbox = gtk.vbox() vbox.pack_start(view,false,false,2) vbox.pack_start(check,false,false,2) vbox.show_all() # pack vbox simple dialog , run dialog = gtk.dialog('treeviewtooltips demo') close = dialog.add_button(gtk.stock_close,gtk.responsetype.none) # add tooltip close button close.set_tooltip_markup('click end demo.') dialog.set_default_size(400,400) dialog.vbox.pack_start(vbox,false,false,2) dialog.run()
who give me elemnt in order put tooltip relative coordinate treeview ? thank advance i'am sorry bad english
you working local coordinates, need refer dialog position root coordinates. issue script main window dialog, therefore in foreground, tooltips appear below (i have fixed deleted code (oops). works, change dialog normal window). general note, widget know parent refer when root coordinates required.
please see modified (but not cleaned up) code below:
#!/usr/bin/env python # coding: utf-8 gi.repository import gtk class treeviewtooltips_gtk3: """tooltip atttach treeview in gtk3 window moving follow row of treeview""" def __init__(self): self.win = gtk.window() self.win.set_decorated(false) self.win.set_default_size(-1, -1) self.label = gtk.label() self.win.add(self.label) # default, tooltip enabled self.__enabled = true def __show(self, tooltip, x, y): """in order move tooltip near row of treeview""" self.label.set_markup(tooltip) w,h = self.win.get_preferred_width() # move window self.win.move(*self.location(x,y,w,h)) # show self.win.show_all() return self.win def __leave_handler(self, view, event): """when pointer leaves view, hide tooltip""" self.win.hide() def enable(self): 'enable tooltip' self.__enabled = true def disable(self): 'disable tooltip' self.__enabled = false def location(self, x, y, w, h): '''given x,y coordinates of pointer , width , height (w,h) demensions of tooltip window, return x, y coordinates of tooltip window. default location center window on pointer , 4 pixels below it. # en clair c'est un décalage de position de la fenetre qui s'ouvre qui donne l'info ''' return x - w/2, y + 4 def tooltip_callback(self,treeview, x, y, keyboard_mode, tooltip): """ in order collect miscellaneous elemnt build data row of treeview""" """3eme règle implementer ça pour afficher un tooltip customisé en fonction des data du tree view""" root_x, root_y = self.dialog.get_position() #get root coordinates x, y = treeview.convert_widget_to_bin_window_coords(x, y) if not self.__enabled : """if don't want tooltip display return false !!!!""" return false try: path, column, cell_x, cell_y = treeview.get_path_at_pos(x, y) iter = model.get_iter(path) #print 'tooltip=',self.get_tooltip(column, self.cust_col,path) #add root coordinates local coordinates treeview.set_tooltip_window(self.__show(self.get_tooltip(column, self.cust_col,path),root_x+x,root_y+y)) # necessary return true in order display window tooltip print("path valid :-)") return true except: # prevent case when path it's not valid print("perhaps path it's not valid ????") # not possible display return false def add_view(self, view): """add gtk.treeview tooltip check if view instance of gtk.treeview , build connector , tooltip enable""" assert isinstance(view, gtk.treeview), \ ('this handler should connected ' 'instances of gtk.treeview') # first condition in gtk3 # set true property "has-tooltip" view.set_property("has-tooltip", true) # second condition in gtk3 view.connect('query-tooltip',self.tooltip_callback) # hide tooltip when mouse out of widget view.connect("leave-notify-event", self.__leave_handler) def get_tooltip(self, view, column, path): """in order secure customized implementation in projet""" 'see module doc string description of method' raise notimplemented('subclass must implement get_tooltip()') if __name__ == '__main__': class demotips_gtk3(treeviewtooltips_gtk3): """demo play how """ def __init__(self, customer_column): """ init class customer_column instance of gtk.treeviewcolumn , being used in gtk.treeview show customer names.""" self.cust_col = customer_column # call base class init treeviewtooltips_gtk3.__init__(self) def get_tooltip(self, column,cust_col,path): """by checking both column , path have cell-based tooltip""" model = view.get_model() customer = model[path][2] if column cust_col: """here there lot of information on row""" return '<big>%s %s</big>\n<i>%s</i>' % (customer.fname, customer.lname, customer.notes) else: """here basic data !!!!""" return ('<big><u>generic column tooltip</u></big>\n' 'unless otherwise noted, all\n phone number %s ') % customer.phone # here's our customer class customer: def __init__(self, fname, lname, phone, notes): self.fname = fname self.lname = lname self.phone = phone self.notes = notes # create bunch of customers customers = [] fname, lname, phone, notes in [ ('joe', 'schmoe', '555-1212', 'likes morris dance.'), ('jane', 'doe', '555-2323', 'wonders hell\nmorris dancing is.'), ('phred', 'phantastic', '900-555-1212', 'dreams of betty.'), ('betty', 'boop', '555-3434', 'dreams in b&w.'), ('red sox', 'fan', '555-4545', "still livin' 2004!\nespecially after 2006.")]: customers.append(customer(fname, lname, phone, notes)) # build our model , view model = gtk.liststore(str, str, object) c in customers: model.append(['%s %s' % (c.fname, c.lname), c.phone, c]) view = gtk.treeview(model) # 2 columns, name , phone cell = gtk.cellrenderertext() cell.set_property('xpad', 20) namecol = gtk.treeviewcolumn('customer name', cell, text=0) namecol.set_min_width(200) view.append_column(namecol) cell = gtk.cellrenderertext() phonecol = gtk.treeviewcolumn('phone', cell, text=1) view.append_column(phonecol) # finally, connect tooltip, specifying name column # column want tooltip popup over. tips = demotips_gtk3(namecol) tips.add_view(view) # we're going demonstrate enable/disable. first need # callback function connect toggled signal. def toggle(button): if button.get_active(): tips.disable() else: tips.enable() # create checkbutton , connect our handler check = gtk.checkbutton('check disable view tooltips') check.connect('toggled', toggle) check.set_tooltip_markup('this standard gtk tooltip.\n' 'compare me tooltips above.') # create vbox pack view , checkbutton vbox = gtk.vbox() vbox.pack_start(view,false,false,2) vbox.pack_start(check,false,false,2) vbox.show_all() # pack vbox simple dialog , run dialog = gtk.dialog('treeviewtooltips demo') tips.dialog = dialog #give tips main window reference close = dialog.add_button(gtk.stock_close,gtk.responsetype.none) # add tooltip close button close.set_tooltip_markup('click end demo.') dialog.set_default_size(400,400) dialog.vbox.pack_start(vbox,false,false,2) dialog.run()
Comments
Post a Comment