How can I fix _tkinter.TclError: invalid command name ".!entry" error with Python-Requests API in my Tkinter code?

huangapple go评论68阅读模式
英文:

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('未找到')

请注意,我已经将代码中的 &quot; 替换为正常的引号和字符。

英文:

This is the error I'm getting:

_tkinter.TclError: invalid command name &quot;.!entry&quot;

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(&quot;icon\ico3\hp_download.ico&quot;)
window.title(&quot;Universitis Of IRAN&quot;)
# w,h=window.winfo_screenwidth(),window.winfo_screenheight()
# window.geometry(&quot;%dx%d+%d+%d&quot;%(w,h,0,0))
window.geometry(&#39;400x400&#39;)
def show(event):
    root=tk.Tk()
    def func(e):
        print(type(province.get()))
        # root.destroy()
    Button=tk.Button(root,text=&quot;Return&quot;,width=30)
    Button.bind(&quot;&lt;Button&gt;&quot;,func)
    Button.grid(row=2,column=2)
    root.mainloop()
label=tk.Label(window,text=&quot; Enter the province : &quot;,width=30)
province=tk.Entry(window,width=30)
showButton=tk.Button(window,text=&quot;Show&quot;,width=30)
showButton.bind(&quot;&lt;Button&gt;&quot;,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={&#39;country&#39;:&#39;IRAN&#39;}
res=requests.get(&#39;http://universities.hipolabs.com/search?&#39;,land)
if res.status_code==200:
    temp=res.json()
    stateStr=province.get()
    for i in range(len(temp)):
        if temp[i][&quot;state-province&quot;]==stateStr:
            print(temp[i][&quot;name&quot;])
            print(&quot;web address : &quot;,temp[i][&quot;web_pages&quot;])
        else:
            print(&#39;no&#39;)
    

else:
    print(&#39;not found&#39;)

答案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(&#39;http://universities.hipolabs.com/search?&#39;, {&#39;country&#39;: &#39;IRAN&#39;})
    if res.status_code == 200:
        temp = res.json()
        stateStr = province.get()
        for i in range(len(temp)):
            if temp[i][&quot;state-province&quot;] == stateStr:
                print(temp[i][&quot;name&quot;])
                print(&quot;web address : &quot;, temp[i][&quot;web_pages&quot;])
            else:
                print(&#39;no&#39;)
    else:
        print(&#39;not found&#39;)

window = tk.Tk()
window.title(&quot;Universities Of IRAN&quot;)
window.geometry(&#39;400x400&#39;)

label = tk.Label(window,text=&quot;Enter the province : &quot;,width=30)
province = tk.Entry(window,width=30)
showButton = tk.Button(window, text=&quot;Show&quot;, 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()

huangapple
  • 本文由 发表于 2023年5月30日 00:26:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定