1

I am working on a mini GUI project , I am currently struggling to figure out how to get selected value from the list and then return that value to the main function so that I can use that value in somewhere else . Can someone help me please !!!!

    ####

    self.device_list_store = gtk.ListStore(str,str,str,str,str)
    for device in self.get_dev_list():
        self.device_list_store.append(list(device))

    device_list_treeview = gtk.TreeView(self.device_list_store)

    selected_row = device_list_treeview.get_selection()
    selected_row.connect("changed",self.item_selected)

    ####

    def item_selected(self,selection):
         model,row = selection.get_selected()
         if row is not None:
             selected_device = model[row][0]

at the moment ,the item_selected function is not returning anything , I want to return selected_device back to the main function so I can use it in other functions as well .

EDIT: I've edited code above to remove formatting errors @jcoppens

jcoppens
  • 5,306
  • 6
  • 27
  • 47
Dennis.Z
  • 47
  • 3
  • why not put `return selected_device` as last line of the `item_selected` function. And have a read of [some basic Python tutorials](https://docs.python.org/3/tutorial/index.html) and [Gtk tutorials](https://python-gtk-3-tutorial.readthedocs.io/)? – jcoppens Jul 11 '17 at 04:56
  • Thanks for your reply , I tried return selected_device , and call the item_selected function in the main function , it doesn't work , the error msg says TypeError: item_selected() takes exactly 2 arguments (1 given). I can understand I need to pass some arguments to that function , but the problem is I have no idea what parameters I need to use for that function , hopefully this makes sense to you . – Dennis.Z Jul 11 '17 at 05:07

1 Answers1

0

As you can see in the documentation, the item_selected function is called with one parameter, tree_selection. But if you define the function inside a class, it requires the self parameter too, which is normally added automatically. In your (confusing) example, there is no class defined, so I suspect the problem is your program which is incomplete.

Also, I suspect you don't want device_list_treeview = gtk.T... in the for loop:

for device in self.get_dev_list():
        self.device_list_store.append(list(device))
        device_list_treeview = gtk.TreeView(self.device_list_store)

And I suspect you want selected_device = mod... indented below the if:

 if row is not None:
 selected_device = model[row][0]

Please convert your example in a complete program, and formatted correctly.

BTW: item_selected is not a good name for the signal handler. It is also called if the item is unselected (which is why the signal is called 'changed')!

And important: Even though you should first read the basic Python tutorials and Gtk tutorials, you should then consider using lazka's excellent reference for all the Python APIs. There's a link on the page to download it completely and have it at hand in your computer.

jcoppens
  • 5,306
  • 6
  • 27
  • 47
  • Thank you so much , the issue has been solved now . I will have a read of the training materials that you suggested to make sure I have all those basic stuff covered . – Dennis.Z Jul 11 '17 at 23:04
  • Great. I've edited your original question to remove the formatting problems. – jcoppens Jul 12 '17 at 14:21