Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning the month index/number instead of month name

self.months = ('January', 'February', 'March',
                        'April', 'May', 'June', 'July', 'August',
                        'September', 'October', 'November', 'December')

self.option_var = tk.StringVar(self)

        # option menu
        option_menum = ttk.OptionMenu(
            self,
            self.option_var,
            self.months[0],
            *self.months,
            )

birth_month = self.option_var.get()

so, I’m programming an age calculator in python + tkinter and I’m wondering if instead of returning the month name ("Jan"…), I can return a number, or rather the month’s index in the list.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How about making self.months a list?

self.months = ['January', 'February', 'March',
                        'April', 'May', 'June', 'July', 'August',
                        'September', 'October', 'November', 'December'] 

and then use .index(), e.g.

month_you_are_looking_for = "March"

month_as_integer = self.months.index(month_you_are_looking_for)+1

As @h4z3 pointed out in the comments, the same works if you want to use your months in a tuple, as you originally did!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading