我正在開發一個迷你GUI項目,我正在努力弄清楚如何從列表中獲取選定值,然後將該值返回給主函數,以便我可以使用該函數價值在別的地方。有人能幫助我嗎 !!!!Python GTK從樹形視圖中獲取選定的值
####
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'函數的最後一行。並閱讀[一些基本的Python教程](https://docs.python.org/3/tutorial/index.html)和[Gtk教程](https://python-gtk-3-tutorial.readthedocs。 IO /)? – jcoppens
感謝您的回覆,我試着返回selected_device,並調用主函數中的item_selected函數,它不起作用,錯誤消息說TypeError:item_selected()只需要2個參數(給出1)。我可以理解,我需要將一些參數傳遞給該函數,但問題是我不知道該函數需要使用哪些參數,希望這對您有意義。 –