Reputation: 157
It runs well but when I close the window a keyError: ' ' appears. It is getting the values from the key selected but I don´t see why is giving me that. I would appreciate some help here. I clarify that the dictionaries are string:string and I'm concatenating to get a string variable to then do some operations. I add that combo1 and combo2 are strings fixed. The issue is that it concatenate in var2 and works but after that appear this error.
from tkinter import *
from tkinter import ttk
import sqlite3
class dd:
def __init__(self, window):
self.wind = window
def setvalue(event):
var = str(self.combo1.get()) + "-" + str(self.combo2.get()) + "-"
var2 = self.get_eln()[self.combo3.get()] + "-" + self.get_esr([self.combo4.get()]
self.var3 = var + var2
frame = LabelFrame(self.wind, text='Data', height = 50, width = 1000, bd=4)
frame.grid(row=0, column=0, sticky=W+E)
frame.grid_propagate(0)
#Combo box 3
self.combo3 = ttk.Combobox(frame, value=list(self.get_eln().keys()), width=60)
self.combo3.bind("<<ComboboxSelected>>", setvalue)
self.combo3.grid(row=1, column=4, pady=4, sticky=W)
#Combo box 4
self.combo4 = ttk.Combobox(frame, value=list(self.get_esr().keys()),width=20)
self.combo4.bind("<<ComboboxSelected>>", setvalue)
self.combo4.grid(row=1, column=6, pady=4, sticky=W)
def run_query(self, query, parameters = ()):
with sqlite3.connect(self.db) as conn:
cursor = conn.cursor()
result = cursor.execute(query, parameters)
conn.commit()
return result
def get_eln(self):
query = 'SELECT Eln, E_a FROM Elns'
db_rows = self.run_query(query)
return {Eln:E_a for Eln, E_a in db_rows}
def get_esr(self):
query = 'SELECT Esr, Est_a FROM Ess'
db_rows = self.run_query(query)
return {Esr:Est_a for Esr,Est_a in db_rows}
if __name__ == '__main__':
window = Tk()
application = a(window)
window.mainloop()
When I close the window appears this:
Exception in Tkinter callback
Traceback (most recent call last):
File "Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "code.py", line 6, in setvalue
var2 = self.get_eln()[self.combo3.get()] + "_" + self.get_esr()[self.combo4.get()]
KeyError: ''
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "code.py", line 6, in setvalue
var2 = self.get_eln()[self.combo3.get()] + "_" + self.get_esr()[self.combo4.get()]
KeyError: ''
I believe the error is in the concatenation of var 2.
Upvotes: 1
Views: 1380
Reputation: 139
Like I said in the comment, the problem seems to be from trying to get the value from the dict
returned from self.get_eln()
and self.get_esr()
when the self.combo3
or self.combo4
has no value.
I guess you could either check if both have values before trying to do so like this:
if self.combo3.get() and self.combo4.get():
var2 = self.get_eln()[self.combo3.get()] + "-" + self.get_esr([self.combo4.get()]
else:
var2 = '' # not sure which value you want for this var
and this way you ensure you don't get a KeyError
.
Otherwise, if you wish to follow another approach with try ... except
you could always do something like
try:
var2 = self.get_eln()[self.combo3.get()] + "-" + self.get_esr([self.combo4.get()]
except KeyError:
# make sure you don't want to
var2 = ''
I don't want to give you a simple copy-paste answer, so if you don't understand something, just comment and I'll try to help!
Upvotes: 1