2021年5月4日星期二

KeyError raised when attempting to use Tkinter entry to call a request

I'm fairly new to programming and Python in general. I'm attempting to use Tkinter to build a bible app for my mom as a present of sorts for mothers day, however I'm mainly using this project as a means to practice and develop new skills. I'm using the bible-api and JSON to return the requested verse (For example http://bible-api.com/john 3:16 will return said verse). However, whenever I attempt to run the program, I receive a key error, in specific "KeyError: 'text'" with a traceback to lines 21 and 8. Code is as follows:

import json  import requests  from tkinter import *        def printVerse():      verse_input = entry.get()      verse_raw = requests.get('https://bible-api.com/' + verse_input).json()      verse_text = verse_raw['text']      verse_output = Label(root, text=verse_text)      verse_output.pack()    root = Tk()  root.title('Bible App')    root.geometry('1000x500')    entry = Entry(root)  entry.insert(0, "Ex. 'John 3:16'")  entry.pack(padx=5, pady=5)    return_button = Button(root, text='Print Your Verse!', command=printVerse())  return_button.pack()      root.mainloop()  

Whenever I replace entry.get() with a regular string the program executes with no issues. I've also tried adding a textvariable to Entry(root) and using the .get command on the entry variable, however that returns the same error. What am I doing wrong here?

Thanks in advance.

https://stackoverflow.com/questions/67393946/keyerror-raised-when-attempting-to-use-tkinter-entry-to-call-a-request May 05, 2021 at 09:04AM

没有评论:

发表评论