alejandroautalan / pygubu

A simple GUI builder for the python tkinter module
MIT License
2.03k stars 212 forks source link

EditableTreeView - cells not selected #73

Closed tcareynokia closed 8 years ago

tcareynokia commented 8 years ago

Hi,

I was thinking of using the editable tree view was wrapped in a scrollbar helper with an inplace edit of list of items (2 columns).

When I select the first item - it allows me to edit the cell. However that only occurs on the first click. If I edit the cell and then select another item - I can only select the row not the cell.

Any help would be appreciated as I would like to use the inplace editting.

alejandroautalan commented 8 years ago

Hello tcareynokia, thanks for trying pygubu.

I did not quite understand your question but here are some notes about the EditableTreeview widget.

Custom Events Generated: <> Used to configure the widget editors for each cell. <> Generated when a column value was changed.

Internally uses the <> event, so if you also want to use it, make sure that the 'add' checkbox is checked in the Bindings panels.

It has the following methods to define the type of the cell editor: inplace_entry, inplace_checkbutton, inplace_combobox, inplace_spinbox and inplace_custom

Here is an example of use. If you need a more elaborated example or have other questions, please let me know.

Regards Alejandro A.

File editabletv.ui

<?xml version='1.0' encoding='utf-8'?>
<interface>
  <object class="tk.Toplevel" id="mainwindow">
    <property name="height">200</property>
    <property name="resizable">both</property>
    <property name="title" translatable="yes">Editable Treeview Example</property>
    <property name="width">200</property>
    <child>
      <object class="ttk.Frame" id="Frame_1">
        <property name="height">200</property>
        <property name="width">200</property>
        <layout>
          <property name="column">0</property>
          <property name="propagate">True</property>
          <property name="row">0</property>
          <property name="sticky">nsew</property>
          <rows>
            <row id="0">
              <property name="weight">1</property>
            </row>
          </rows>
          <columns>
            <column id="0">
              <property name="weight">1</property>
            </column>
          </columns>
        </layout>
        <child>
          <object class="ttk.Frame" id="Frame_2">
            <property name="height">200</property>
            <property name="padding">0 5</property>
            <property name="width">200</property>
            <layout>
              <property name="column">0</property>
              <property name="propagate">True</property>
              <property name="row">0</property>
              <property name="sticky">ew</property>
              <rows>
                <row id="0">
                  <property name="weight">0</property>
                </row>
              </rows>
              <columns>
                <column id="0">
                  <property name="weight">0</property>
                </column>
              </columns>
            </layout>
            <child>
              <object class="ttk.Checkbutton" id="Checkbutton_1">
                <property name="text" translatable="yes">Allow Edit</property>
                <property name="variable">boolean:allow_edit</property>
                <layout>
                  <property name="column">0</property>
                  <property name="propagate">True</property>
                  <property name="row">0</property>
                </layout>
              </object>
            </child>
          </object>
        </child>
        <child>
          <object class="pygubu.builder.widgets.scrollbarhelper" id="scrollbarhelper_1">
            <property name="scrolltype">both</property>
            <layout>
              <property name="column">0</property>
              <property name="propagate">True</property>
              <property name="row">1</property>
              <property name="sticky">nsew</property>
            </layout>
            <child>
              <object class="pygubu.builder.widgets.editabletreeview" id="myetv">
                <property name="show">headings</property>
                <bind add="" handler="on_cell_changed" sequence="&lt;&lt;TreeviewCellEdited&gt;&gt;" />
                <bind add="" handler="on_row_edit" sequence="&lt;&lt;TreeviewInplaceEdit&gt;&gt;" />
                <bind add="True" handler="on_row_selected" sequence="&lt;&lt;TreeviewSelect&gt;&gt;" />
                <layout>
                  <property name="column">0</property>
                  <property name="propagate">True</property>
                  <property name="row">0</property>
                  <property name="sticky">nsew</property>
                </layout>
                <child>
                  <object class="ttk.Treeview.Column" id="name">
                    <property name="column_anchor">w</property>
                    <property name="heading_anchor">w</property>
                    <property name="minwidth">20</property>
                    <property name="stretch">true</property>
                    <property name="text" translatable="yes">Name</property>
                    <property name="tree_column">false</property>
                    <property name="visible">true</property>
                    <property name="width">200</property>
                  </object>
                </child>
                <child>
                  <object class="ttk.Treeview.Column" id="url">
                    <property name="column_anchor">w</property>
                    <property name="heading_anchor">w</property>
                    <property name="minwidth">20</property>
                    <property name="stretch">true</property>
                    <property name="text" translatable="yes">URL</property>
                    <property name="tree_column">false</property>
                    <property name="visible">true</property>
                    <property name="width">200</property>
                  </object>
                </child>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

File editabletv.py

import os
import tkinter as tk
import pygubu

FILE_PATH = os.path.dirname(os.path.abspath(__file__))
UI = os.path.join(FILE_PATH, 'editabletv.ui')

class MyApplication(object):

    def __init__(self, master):
        builder = pygubu.Builder()
        builder.add_from_file(UI)
        self.mainwindow = builder.get_object('mainwindow')
        self.etv = builder.get_object('myetv')

        # Connect bindings
        builder.connect_callbacks(self)
        # Get the variable allow_edit defined in UI file
        builder.import_variables(self, ('allow_edit',))

        # Add some data to the treeview
        data = [
            ('news', 'http://www.tn.com.ar'),
            ('games', 'https://www.gog.com'),
            ('search', 'https://duckduckgo.com')
            ]
        for d in data:
            self.etv.insert('', tk.END, values=d)

    def on_row_edit(self, event):
        # Get the column id and item id of the cell
        # that is going to be edited
        col, item = self.etv.get_event_info()

        # Allow edition only if allow_edit variable is checked
        if self.allow_edit.get() == True:
            # Define the widget editor to be used to edit the column value
            if col in ('url',):
                self.etv.inplace_entry(col, item)

    def on_cell_changed(self, event):
        col, item = self.etv.get_event_info()
        print('Column {0} of item {1} was changed'.format(col, item))

    def on_row_selected(self, event):
        print('Rows selected', event.widget.selection())

    def run(self):
        self.mainwindow.mainloop()

if __name__ == '__main__':
    app = MyApplication(None)
    app.run()
tcareynokia commented 8 years ago

Sorry about that formatting. Yes indeed this solves my problem I missed the TreeviewInplaceEdit event