英文:
How can I fix _tkinter.TclError: invalid command name ".!entry" error with Python-Requests API in my Tkinter code?
问题
这是我得到的错误消息:
_tkinter.TclError: 无效的命令名称 ".!entry"
我期望有人在输入框中输入州的名称,在我的代码中,我获取该字符串,将其发送到 http://universities.hipolabs.com/search,并获取结果。然而,我得到了上面的错误。如果我使用 input()
获取州的名称,那么代码可以正常工作。
import tkinter as tk
window=tk.Tk()
# window.iconbitmap("icon\ico3\hp_download.ico")
window.title("伊朗大学")
# w,h=window.winfo_screenwidth(),window.winfo_screenheight()
# window.geometry("%dx%d+%d+%d"%(w,h,0,0))
window.geometry('400x400')
def show(event):
root=tk.Tk()
def func(e):
print(type(province.get()))
# root.destroy()
Button=tk.Button(root,text="返回",width=30)
Button.bind("<Button>",func)
Button.grid(row=2,column=2)
root.mainloop()
label=tk.Label(window,text="输入州名:",width=30)
province=tk.Entry(window,width=30)
showButton=tk.Button(window,text="显示",width=30)
showButton.bind("<Button>",show)
label.grid(row=0,column=0)
province.grid(row=0,column=1)
showButton.grid(row=2,column=0,columnspan=2)
window.mainloop()
import requests,json
land={'country':'伊朗'}
res=requests.get('http://universities.hipolabs.com/search?',land)
if res.status_code==200:
temp=res.json()
stateStr=province.get()
for i in range(len(temp)):
if temp[i]["state-province"]==stateStr:
print(temp[i]["name"])
print("网址:",temp[i]["web_pages"])
else:
print('无结果')
else:
print('未找到')
请注意,我已经将代码中的 "
替换为正常的引号和字符。
英文:
This is the error I'm getting:
_tkinter.TclError: invalid command name ".!entry"
I expect someone to enter the state in entry and in my code I get that string, send it to http://universities.hipolabs.com/search, and get the result. However, I'm getting the error above. If I get the name of the state with input()
the code works.
import tkinter as tk
window=tk.Tk()
# window.iconbitmap("icon\ico3\hp_download.ico")
window.title("Universitis Of IRAN")
# w,h=window.winfo_screenwidth(),window.winfo_screenheight()
# window.geometry("%dx%d+%d+%d"%(w,h,0,0))
window.geometry('400x400')
def show(event):
root=tk.Tk()
def func(e):
print(type(province.get()))
# root.destroy()
Button=tk.Button(root,text="Return",width=30)
Button.bind("<Button>",func)
Button.grid(row=2,column=2)
root.mainloop()
label=tk.Label(window,text=" Enter the province : ",width=30)
province=tk.Entry(window,width=30)
showButton=tk.Button(window,text="Show",width=30)
showButton.bind("<Button>",show)
label.grid(row=0,column=0)
province.grid(row=0,column=1)
showButton.grid(row=2,column=0,columnspan=2)
window.mainloop()
import requests,json
land={'country':'IRAN'}
res=requests.get('http://universities.hipolabs.com/search?',land)
if res.status_code==200:
temp=res.json()
stateStr=province.get()
for i in range(len(temp)):
if temp[i]["state-province"]==stateStr:
print(temp[i]["name"])
print("web address : ",temp[i]["web_pages"])
else:
print('no')
else:
print('not found')
答案1
得分: 1
你正在尝试在tkinter
主循环结束后访问Entry
对象的province
属性,然后所有的小部件都会被清除,无法再访问。
让我们尝试在Tkinter
主循环的范围内发送请求并处理结果。
import tkinter as tk
import requests
import json
def show():
res = requests.get('http://universities.hipolabs.com/search?', {'country': 'IRAN'})
if res.status_code == 200:
temp = res.json()
stateStr = province.get()
for i in range(len(temp)):
if temp[i]["state-province"] == stateStr:
print(temp[i]["name"])
print("web address : ", temp[i]["web_pages"])
else:
print('no')
else:
print('not found')
window = tk.Tk()
window.title("Universities Of IRAN")
window.geometry('400x400')
label = tk.Label(window, text="Enter the province : ", width=30)
province = tk.Entry(window, width=30)
showButton = tk.Button(window, text="Show", command=show, width=30)
label.grid(row=0, column=0)
province.grid(row=0, column=1)
showButton.grid(row=2, column=0, columnspan=2)
window.mainloop()
英文:
You are trying to access the Entry
object province after the main tkinter
loop has ended, then all the widgets get cleaned up and can't be accessed.
Lets try to send the request and process the result within the scope of the Tkinter
main loop.
import tkinter as tk
import requests
import json
def show():
res = requests.get('http://universities.hipolabs.com/search?', {'country': 'IRAN'})
if res.status_code == 200:
temp = res.json()
stateStr = province.get()
for i in range(len(temp)):
if temp[i]["state-province"] == stateStr:
print(temp[i]["name"])
print("web address : ", temp[i]["web_pages"])
else:
print('no')
else:
print('not found')
window = tk.Tk()
window.title("Universities Of IRAN")
window.geometry('400x400')
label = tk.Label(window,text="Enter the province : ",width=30)
province = tk.Entry(window,width=30)
showButton = tk.Button(window, text="Show", command=show, width=30)
label.grid(row=0, column=0)
province.grid(row=0, column=1)
showButton.grid(row=2, column=0, columnspan=2)
window.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论