How to get text DropDown?

This is my code:

from anvil import *

class Form1(Form1Template):

  def __init__(self, **properties):
    # You must call self.init_components() before doing anything else in this function
    self.init_components(**properties)

    # Any code you write here will run when the form opens.
    price_per_years = [
      ("2002", 1), 
      ("2003", 2)
    ]
    self.year_drop_down.items = price_per_years

  def calculate_button_click (self, **event_args):
    # This method is called when the button is clicked
    
    selected_year_str = self.year_drop_down.text
    print 'selected_year_str ' + str(selected_year_str) + ' ' + str(type(selected_year_str))

After this, I have text 2002 and 2003 in DropDown.
With self.year_drop_down.selected_value I get value 1 or 2 depending on the selected year.
But how do I get what year is selected?
self.year_drop_down.text return an empty string ‘’.

You need “selected_value”.

So, in your example,

self.year_drop_down.selected_value

Thank you on your reply, but I have already tried self.year_drop_down.selected_value
I wrote it in answer:

With self.year_drop_down.selected_value I get value 1 or 2 depending on the selected year.

What I want is to get 2002 or 2003 from the items, that is price_per_years.

price_per_years = [
  ("2002", 1), 
  ("2003", 2)
]
self.year_drop_down.items = price_per_years

Ah, I think I misunderstood.

actually I think I misunderstood again

What value do you want displayed in the drop down, and what value do you want to retrieve on selection?

== Below might not be any use! ==

  def __init__(self, **properties):
    # You must call self.init_components() before doing anything else in this function
    self.init_components(**properties)

    self.price_per_years = {
      "2002" : 1, 
      "2003" : 2
    }
    
    self.year_drop_down.items = self.price_per_years

  def year_drop_down_change (self, **event_args):

    print(self.price_per_years[self.year_drop_down.selected_value])

How about

price_per_years = [
  ("2002", 2002), 
  ("2003", 2003)
]

Or

price_per_years = [ "2002", "2003" ]

with

int(self.year_drop_down.selected_value)
1 Like

You can check it at:
https://KACHQFBO75Z7AF6U.anvilapp.net/L42M6UYTRZ5KJE5EA5KDX2YG

Point is that I have years and prices per each year. The user can select a year from DropDown.
I need to know what year user selected and the corresponding price per that year.
My initial idea was to have one data structure, that would have years and prices per each year.
That is why I started with

price_per_years = [
  ("2002", 1), 
  ("2003", 2)
]
self.year_drop_down.items = price_per_years

This was working fine (years have been in dropdown), but the problem was I did not found a way how to get a year (first element of a tuple) as I said self.year_drop_down.text return an empty string ‘’.

Currently suggested solutions are also not working.

I ended up with this:

price_per_years = [
  ("2002", '2002-1.4'), 
  ("2003", '2003-2.2')
]
self.year_drop_down.items = price_per_years

# how I get price
str(self.year_drop_down.selected_value).split('-')[1]

# how I get year
str(self.year_drop_down.selected_value).split('-')[0]

I personally do not like it, because now I need to put a year in two places in the first element of the tuple and in the second, but it is working.

But if there is some better solution, that somebody has coded/tried in practice. please share it.

I would suggest separating your business logic from the Dropdown configuration. Something like this:

prices = {
  "2002": 1.4
  "2003": 2.2
}

self.year_drop_down.items = prices.keys()

# Get the selected data:
year = self.year_drop_down.selected_value
price = prices[self.year_drop_down.selected_value]

Hope that helps!

1 Like

Sorry to dig up this corpse, but is there no way to get both the selected value and the displayed text from a drop down without maintaining a separate list?

Short answer: No, the text isn’t exposed in selected_value. But you can store as much information in the value as you want to. Eg:

# The "value" for each item is a (name,value) tuple
self.year_drop_down.items = [
  ("2002", ('2002', 1.4)), 
  ("2003", ('2003', 2.2))
]

# ...and then later...
name, value = self.year_drop_down.selected_value
2 Likes

Ah, of course.

Thanks :slight_smile: