【发布时间】:2017-07-11 04:47:50
【问题描述】:
我正在开发一个迷你 GUI 项目,我目前正在努力弄清楚如何从列表中获取选定的值,然后将该值返回给主函数,以便我可以在其他地方使用该值。谁能帮帮我!!!
####
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]
目前,item_selected 函数没有返回任何内容,我想将 selected_device 返回到主函数,这样我也可以在其他函数中使用它。
编辑:我已经编辑了上面的代码以删除格式错误@jcoppens
【问题讨论】:
-
为什么不把
return selected_device作为item_selected函数的最后一行。并阅读some basic Python tutorials 和Gtk tutorials? -
感谢您的回复,我尝试返回 selected_device ,并在主函数中调用 item_selected 函数,它不起作用,错误消息显示 TypeError: item_selected() 恰好需要 2 个参数(1 个给定)。我可以理解我需要向该函数传递一些参数,但问题是我不知道该函数需要使用哪些参数,希望这对您有意义。
标签: python python-2.7 python-3.x gtk gtk3