Tkinter Combobox bind causes KeyError: ' ' on initialization

17 hours ago 3
ARTICLE AD BOX

Hi i am trying to write a program for calculations concerning wiresize, fusesize and amperage-ratings.

I have multiple frames each with its specific calculations. The problem i encounter sits in the unit-convertion frame. the goal here is to convert from AWG to mm2 or vice versa.

When i select a value in the combobox (corresponding the unit i want to convert from), i want the converted value of the other unit to be shown in a 'result_label' below.

Here's what i tried:

class UnitFrame(tk.Frame): def __init__(self, parent): super().__init__(parent) self.awg_dict = {'20': '0.50', '18': '0.75', '16': '1.50', '14': '2.50', '12': '4.00', '10': '6.00', '8': '10.00', '6': '13.30', '4': '21.10', '2': '33.60', '0': '53.50', '00': '67.40', '000': '85.00', '0000': '107.20'} self.mm2_dict = {v: k for k, v in self.awg_dict.items()} #AWG to mm2 self.awg_label = ttk.Label(self, text = 'AWG: ') self.awg_label.grid(column = 0, row = 0, **options) self.sel_awg = tk.StringVar() self.awg_comb = ttk.Combobox(self, textvariable = self.sel_awg) self.awg_comb['values'] = list(self.awg_dict.keys()) self.awg_comb['state'] = 'readonly' self.awg_comb.bind('<<ComboboxSelected>>', self.calculate(self.awg_dict, self.sel_awg.get())) self.awg_comb.grid(column = 1, row = 0, **options) self.awg_res_label = ttk.Label(self, text = 'MM2: ') self.awg_res_label.grid(column = 0, row = 1, **options) self.awg_uitk_label = ttk.Label(self, **uitk_options) self.awg_uitk_label.grid(column = 1, row = 1, **options) #simular code for mm2 to AWG convertion def calculate(self, dict_, key_): self.val = dict_[key_] if dict_ == self.awg_dict: self.awg_uitk_label.configure(text = self.val) else: self.mm2_uitk_label.configure(text = self.val)

So i have 2 comboboxes and 2 dictionaires, depending on wich convertion i want to do.

Where the 'keys' are the value of the given unit and the 'values' are the corresponding value in the other unit.

Then i defined a function named calculate that takes the dictionary i want to use and the given value i want to convert as args. the key_ argument is defined by the textvariable linked to the combobox. In the function i configure the text of the label that should show the result.

this function i bind to the '<<ComboboxSelected>>' event.

With this code i get a KeyError: ' ' when i run the script so i guess that because there is nothing selected in the combobox at this point the key_ arg has an empty string so python can't find this key in the dictionary because it doesn't exists.

In the review of my question, it was pointed out to me that my problem is caused because my bind calls de self.calculate function directly, but i don't know how to pass the needed values to the arguments when i just call a reference.

self.awg_comb.bind('<<ComboboxSelected>>', self.calculate(self.awg_dict, self.sel_awg.get()))

i guess my problem could be solved by using a conditional statement in the self.calculate function that checks the value of a textvariable linked to radiobuttons to determine which convertion i want to do so there is no need to pass args but for the sake of usability i would, as mentioned above, like the program to show the converted value in a result label immediately when i select the to-convert value in the combobox

Can somebody here point me in the right direction for a solution?

ps this is my first question here, let me know if and how i can formulate my questions in a better way!

Read Entire Article