Reputation: 9
I'm stuck right now and could really use some help, i've exhausted every resource google could find for me and I still can't figure out how to do what I am attempting. (If its even possible)
In my python program I am using Tkinter in Python 3.5.1 to make a little calculator applet. For the calculator in question I created a CSV file and imported it using csv.DictReader.
import csv
exptable = csv.DictReader(open('C:/Users/SampleInfo.csv'))
result = {}
for column in exptable:
key = column.pop('Current Level')
if key in result:
pass
result[key] = column
Now the part that I simply can't figure out is how to use the information contained WITHIN this imported dictionary. Here is what I am trying so far...
DropDownLevelVar = StringVar()
DropDownLevel = ttk.OptionMenu(root, {column})
DropDownLevel.grid(column=3, row=1)
This is leaving me with...
Error on line 49, in module DropDownLevel = ttk.OptionMenu(root, {column})
TypeError: unhashable type: 'dict'
The CSV Dictionary I am trying to use contains 2 columns of data, "Current Level and Total EXP" See This for what the Data looks like.
What I would like is for the OptionMenu Dropdown list to be populated with the values listed under Current Level within the dictionary.
My goal is to create a super simple calculator that calculates how many of a certain monster I would need to kill to reach my desired level. (If Current level = 55, then 100 Kills at 500xp ea until 56.) I imported the dictionary so that I could reference the values over and over if I needed to.
I'm REALLY new to programming so i'm sorry if I look like a complete idiot!
Upvotes: 1
Views: 428
Reputation: 16082
Why not use this method to populate your dict?
Anyway, to correctly populate the result
dict:
import csv
exptable = csv.DictReader(open('C:/Users/SampleInfo.csv'))
result = {}
for column in exptable: # should actually be called `row`
key = column['Current Level']
if key not in result:
result[key] = column['Total EXP']
The for
and if
block can be better written as:
for column in exptable: # should actually be called `row`
if column['Current Level'] not in result:
result[column['Current Level']] = column['Total EXP']
If ttk.OptionMenu
wants a dict, then the line DropDownLevel = ttk.OptionMenu(root, {column})
should probably be:
DropDownLevel = ttk.OptionMenu(root, result)
Edit: And the pythonic way to do this, as per the method linked above:
import csv
result = {}
with open('C:/Users/SampleInfo.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['Current Level'] not in result: # is this check necessary?
result[row['Current Level']] = row['Total EXP']
Upvotes: 2