Getting the row title of the selected row
Not sure why I'm getting errors here, but I'm trying to get the title of the selected row chosen (or the name of whatever is in the row)
if sender.items[title] == 'Notepad': ui.load_view('Notepad').present() else: pass
I believe you want:
if sender.items['title'] == 'Notepad':
Also, the following code is unnecessary and can safely be removed.
Using @polymerchm's method gives me the following error:
List indices must be integers, not str
type(sender.items)will show you that
sender.itemsis a list.
Lists are indeed indexed by integers, not by strings.
type(title)show you that
titleis a string or an integer?
You could try
if sender.items[int(title)] == 'Notepad':if
titlecontains a string that represents an integer.
type(title) is not defined
type('title') shows a string
Can you post the code, not just the snippet? Might be easier to help.
Same as above post
titleis undefined then why would you expect your code above to work??
As I said in my original post, I am not sure what I need. I want to get the text in the row selected.
JonB last edited by
Keep in mind, none of us here has access to your code, so we are just guessing what you are trying to do,and where you are trying to do it ( are you in a table view action function? Or some button action? We have no idea, and the meaning of sender is different!). Also, in your original post you said you were getting an error... But didn't mention what error you are getting.
So, rather than guess, I'll point you to
which shows an example of how to get the text of the currently selected row, from within the ListDataSource action. See line 45.
@JonB Here is my full program. I am trying to launch different ui.Views based on the TEXT INSIDE OF A CELL.
If I click Notepad, I would like Notepad to launch.
# coding: utf-8 import ui from datetime import datetime, date current_date = datetime.strftime(date.today(), "%A, %B %d, %Y") schedule = 'A Day' def nav(sender): if sender.items[int('title')] == 'Notepad': ui.load_view('Notepad').present() else: pass v = ui.load_view('SoarNet') v['todays_date'].text = ('Today is ' + current_date) v['schedule'].text = schedule v.present(orientations=['landscape'], hide_title_bar=True)
In the main part of your script, you need to add a line like:
v['tableview1'].delegate.action = nav# alternatively, this could be done in the .pyui file
And then the
if sender.items[sender.selected_row]['title'] == 'Notepad':
I'll presume that you are using a tableview to store your selection. Start small and replace the ui.load_view with a print statement to ensure you are actually getting in here. Next, I presume you hardcoded nav as the action to this view in the gui editor. That's okay. "sender" is the returned the tableview object. The "items" are actually in sender.data_source.items. Get the selected row via:
row = sender.selected_row if sender.data_source.items[row]['title'] == 'Notepad': print "got here"
Using NavigationViews is a much easier way to switch between views. Look at my flashcard program @ https://github.com/polymerchm/flashcard to see how I switch between the "flashcard view" and the "dictionary views". Also getting the items and changing them programmatically.
@polymerchm wrote "sender" is the returned the tableview object.
senderis the ui.ListDataSource object, not the ui.TableView object which makes things easier.
Thaanks for the clarification.