I'm getting KeyError: ' ' in my code and I don't see why
I'm getting KeyError: ' ' in my code and I don't see why
它运行良好,但是当我关闭 window 时出现 keyError: ' '。它正在从选定的键中获取值,但我不明白为什么要给我这个值。我会很感激这里的一些帮助。
我澄清字典是 string:string 并且我正在连接以获得一个字符串变量然后执行一些操作。我补充说 combo1 和 combo2 是固定的字符串。问题是它在 var2 中连接并工作但之后出现此错误。
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()
当我关闭 window 时出现:
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: ''
我认为错误在于 var 2 的串联。
就像我在评论中所说的那样,问题似乎出在 self.combo3
时试图从 self.get_eln()
和 self.get_esr()
返回的 dict
中获取值或 self.combo4
没有价值。
我想你可以在尝试这样做之前检查两者是否都有值:
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
这样你就可以确保你不会得到 KeyError
.
否则,如果您希望采用 try ... except
的另一种方法,您可以随时执行类似
的操作
try:
var2 = self.get_eln()[self.combo3.get()] + "-" + self.get_esr([self.combo4.get()]
except KeyError:
# make sure you don't want to
var2 = ''
我不想给你一个简单的copy-paste答案,所以如果你有什么不明白的地方,请发表评论,我会尽力帮助你!
它运行良好,但是当我关闭 window 时出现 keyError: ' '。它正在从选定的键中获取值,但我不明白为什么要给我这个值。我会很感激这里的一些帮助。 我澄清字典是 string:string 并且我正在连接以获得一个字符串变量然后执行一些操作。我补充说 combo1 和 combo2 是固定的字符串。问题是它在 var2 中连接并工作但之后出现此错误。
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()
当我关闭 window 时出现:
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: ''
我认为错误在于 var 2 的串联。
就像我在评论中所说的那样,问题似乎出在 self.combo3
时试图从 self.get_eln()
和 self.get_esr()
返回的 dict
中获取值或 self.combo4
没有价值。
我想你可以在尝试这样做之前检查两者是否都有值:
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
这样你就可以确保你不会得到 KeyError
.
否则,如果您希望采用 try ... except
的另一种方法,您可以随时执行类似
try:
var2 = self.get_eln()[self.combo3.get()] + "-" + self.get_esr([self.combo4.get()]
except KeyError:
# make sure you don't want to
var2 = ''
我不想给你一个简单的copy-paste答案,所以如果你有什么不明白的地方,请发表评论,我会尽力帮助你!