我正在尝试从csv文件中读取NCAA篮球队的数据,但是当我尝试读取“Entry”小部件的用户输入时,python将空字段作为变量并读取csv文件以获取空白空间。
hometeam = tk.StringVar()
awayteam = tk.StringVar()
label1 = ttk.Label(Teams, text="Enter the Home Team", font=LARGE_FONT)
label1.pack(pady=10, padx=10)
entry1 = ttk.Entry(Teams, textvariable=hometeam)
entry1.insert(0, "Home Team")
entry1.pack()
label2 = ttk.Label(Teams, text="Enter the Away Team", font=LARGE_FONT)
label2.pack(pady=10, padx=10)
entry2 = ttk.Entry(Teams, textvariable=awayteam)
entry2.insert(0, "Away Team")
entry2.pack()
def _login_btn_clicked():
hometeam = entry1.get()
awayteam = entry2.get()
if all(k in result for k in (hometeam, awayteam)):
tm.showinfo("Login info", "Welcome John")
raise_frame(Final)
elif awayteam or hometeam not in result:
tm.showerror("Error", "One or more teams are not NCAA Men's Basketball teams")
elif awayteam == hometeam:
tm.showerror("Error", "A team cannot play itself")
button = ttk.Button(Teams, text="Calculate", command=_login_btn_clicked)
button.pack(pady=10, padx=10, side='left')
button1 = ttk.Button(Teams, text="back", command=lambda: raise_frame(Start))
button1.pack(pady=10, padx=10, side='right')
ppg2 = float(result[awayteam][0])
apg2 = float(result[awayteam][1])
rpg2 = float(result[awayteam][2])
bpg2 = float(result[awayteam][3])
spg2 = float(result[awayteam][4])
win_pct2 = float(result[awayteam][5])
rb_pct2 = float(result[awayteam][6])
ast_pct2 = float(result[awayteam][7])
sos2 = float(result[awayteam][8])
fg_pct2 = float(result[awayteam][9])
games2 = float(result[awayteam][10])
ppg1 = float(result[hometeam][0])
apg1 = float(result[hometeam][1])
rpg1 = float(result[hometeam][2])
bpg1 = float(result[hometeam][3])
spg1 = float(result[hometeam][4])
win_pct1 = float(result[hometeam][5])
rb_pct1 = float(result[hometeam][6])
ast_pct1 = float(result[hometeam][7])
sos1 = float(result[hometeam][8])
fg_pct1 = float(result[hometeam][9])
games1 = float(result[hometeam][10])
示例数据文件:
维拉诺瓦,880,161,388,34,74,1,53.7,52.6,6.34,0.49,11
完整代码:
http://pastebin.com/ekpLWvGf
追溯:
Traceback (most recent call last):
File "D:/Files/Code/Python/SPI/Beta/Testgui/c.py", line 70, in <module>
ppg2 = float(result[awayteam.get()][0])
KeyError: 'Away Team'