I want to get a loading dialog box while I call a API in my tkinter application I have implement a custom dialog box in it but the dialog box is not coming basically the whole application freezes and loading dialog box also does not come.
This is a sample function which run when I click on submit button and then this function runs the api call and and open the dialog box while api is making call and also changes frame/navigate to other frame.
def login_button_fuction():
top = tk.Toplevel(self)
top.title("loading")
top.geometry("200x100")
top.resizable(0, 0)
bar = ttk.Progressbar(top, orient="horizontal",
length=100, mode="indeterminate")
bar.pack(pady=25)
bar.start()
url = "XXXXXXXXXXXXXXXXXXX"
payload = json.dumps(
{"username": username_entry.get(), "password": password_entry.get()})
headers = {'Content-Type': 'application/json'}
response = requests.post(url+"/users/getuser", data=payload, headers=headers)
if response.json()["resultCode"] == 100:
print("success")
data = {
"realname": response.json()["data"]["realname"],
"phone": response.json()["data"]["phone"]}
with open("user_data.dat", "wb") as f:
pickle.dump(data, f)
controller.show_frame(dashboardPage.dashboard_page)
else:
print("error")
bar.grid_forget()
top.destroy()
Also if any one can tell me how can I remove the minimize and close buttons from tkinter Dialog Box it will be very helpful. The loading Dialog Box Should come while making a API call and should automatically close when the Api call has been made.
I tried this but it hangs whole application and crashes it.
class login_page(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#functions for buttons
def try_login():
url = "XXXXXXXXXXXXXXX"
payload = json.dumps(
{"username": username_entry.get(), "password": password_entry.get()})
headers = {'Content-Type': 'application/json'}
response = requests.post(
url+"/users/getuser", data=payload, headers=headers)
return response
def login_button_fuction():
top = tk.Toplevel(self)
top.title("loading")
top.geometry("200x100")
top.resizable(0, 0)
bar = ttk.Progressbar(top, orient="horizontal",
length=100, mode="indeterminate")
bar.pack(pady=25)
bar.start()
response = threading.Thread(target=try_login)
response.start()
response.join()
print(response)
if response.json()["resultCode"] == 100:
print("success")
data = {
"realname": response.json()["data"]["realname"],
"phone": response.json()["data"]["phone"],
"email": response.json()["data"]["email"]}
with open("user_data.dat", "wb") as f:
pickle.dump(data, f)
controller.show_frame(dashboardPage.dashboard_page)
else:
print("error")
bar.grid_forget()
top.destroy()