Question

I have created TreeView() table with pygtk in my script. In that one column textbox is as password type and if I type password it is showing character but password should be confidential. so I want, when I type password it show me just like as invisible (*).

My code is written below

    #   columns
    (
      COLUMN_EDITABLE,
      COLUMN_USER,
      COLUMN_PSW,
    ) = range(3)
    model = gtk.ListStore(
       gobject.TYPE_BOOLEAN,
       gobject.TYPE_STRING,
       gobject.TYPE_STRING,
    )
    treeview = gtk.TreeView(model)

    #Password column
    renderer = gtk.CellRendererText()
    renderer.set_property('cell-background', '#efefef')
    renderer.set_fixed_size(120,20)
    renderer.connect("edited", self.on_cell_edited, model)
    renderer.set_data("column", COLUMN_PSW)
    column = gtk.TreeViewColumn("Password", renderer, text=COLUMN_PSW,
                           editable=COLUMN_EDITABLE)
    treeview.append_column(column)

Now any one suggest me for this problem, what i do

Thanks in advance..

Was it helpful?

Solution

You need to connect to the editing-started signal of the text renderer and set the visibility property of the GtkEditable to False in the handler. To avoid displaying the password when the user has finished editing it you should set the text property to a fixed string and clear the contents of the editable in the editing-started handler.

def on_cell_edited_started(self, cell, editable, path):
    editable.delete_text(0, -1)
    editable.set_visibility(False)

and then in __init__

self.renderer.set_property("text", "*****")
self.renderer.connect("editing-started", self.on_cell_editing_started)

Update:

I cannot reproduce your problem with the text showing after the ***. I've modified your code as follows. when I click to edit the text is cleared and when I finish editing it goes back to ***

Callbacks:

def on_cell_editing_started(self,cell, editable, path):
    editable.delete_text(0,-1)
    editable.set_visibility(False)
def on_cell_edited(self, renderer, path, new_text, model):
    iter = model.get_iter(path)
    model.set_value(iter, COLUMN_PSW, new_text)

Setting up the renderer:

    # psw column
    self.renderer = gtk.CellRendererText()
    self.renderer.connect("editing-started", self.on_cell_editing_started)
    self.renderer.connect("edited", self.on_cell_edited, model)
    self.renderer.set_property("text", "***")
    column = gtk.TreeViewColumn("Password", self.renderer ,editable=COLUMN_EDITABLE)
    treeview.append_column(column)  
    sw.add(treeview)
    self.show_all()

Everything else is as in the latest file you shared. I suspect you've somehow set the text property of the renderer with set_property and also bound a column to it as well. I'm going to be off-line for the next 10 days or so, if your still having problems have a careful read of the documentation and double check you code so you understand what each line is doing.

OTHER TIPS

The following is an anwser to the above question but using the language c#.

I did some investigation and found on the gtkmm documentation page the following code

void ExampleWindow::cellrenderer_validated_on_editing_started(
    Gtk::CellEditable* cell_editable, const Glib::ustring& /* path */)
{
   //Start editing with previously-entered (but invalid) text, 
   //if we are allowing the user to correct some invalid data. 
   if(m_validate_retry)
   {
      //This is the CellEditable inside the CellRenderer. 
      Gtk::CellEditable* celleditable_validated = cell_editable;

      //It's usually an Entry, at least for a CellRendererText:
      Gtk::Entry* pEntry = dynamic_cast<Gtk::Entry*>(celleditable_validated);
      if(pEntry)
      {
         pEntry->set_text(m_invalid_text_for_retry);
         m_validate_retry = false;
         m_invalid_text_for_retry.clear();
      }
   }
}

It states that the underlying editable can be casted into an gtk entry. Using this entry you can easily set the visibility to false and the invisible char to "*".

So to get everything together:

  • Set the celltextrenderer to a default string "***" on init
  • hook the event editing started
    • inside the handler cast the editable to the gtk entry
    • clear the gtk entry
    • set the visibility to false on the entry
    • set the invisible char to "*"
  • hook the event edited
    • extract the new value and store it.

Below the solution in c#:

// INIT
_textRenderer = new CellRendererText();
_textRenderer.Editable = true;
_textRenderer.EditingStarted += TextEditingStarted;
_textRenderer.Edited += TextEdited;
_textRenderer.Text = "***";

private void TextEditingStarted(object o, EditingStartedArgs args)
{
  if (args.Editable is Entry entry)
  {
    entry.InvisibleChar = '*';
    entry.Visibility = false;
    entry.Text = string.Empty;
  }
}

private void TextEdited(object o, EditedArgs args)
{
   var s = args.NewText;
   Console.WriteLine($"Password is {s}");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top