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')
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()