fromtkinterimportmessageboxmessagebox.showinfo(title="Info",message="Hey this is info message")messagebox.showwarning(title="Warning",message="Hey this is warning message")messagebox.showerror(title="Error",message="Hey this is error message")
Simple Message box to get feedback from user
importtkinterastkfromtkinterimportmessagebox# this will retun yes/no as stringresult=messagebox.askquestion("askquestion","Are you sure?")print(result)# this will retun True/Falue as boolresult=messagebox.askokcancel("askokcancel","Want to continue?")print(result)# this will retun True/Falue as boolresult=messagebox.askyesno("askyesno","Find the value?")print(result)# this will retun True/Falue as boolresult=messagebox.askretrycancel("askretrycancel","Try again?")print(result)
Simple Window with message
importtkinter# Create new window# nothing will happen when you run this code# we need somethign to keep our window runningwindow=tkinter.Tk()# Code for GUIwindow.title("Results")window.minsize(width=500,height=150)label=tkinter.Label(text="Hey, you've successfully completed this test",font=("Arial",12,"bold"))label.pack(side="left")label.pack(anchor="nw")# this is require to keep window runningwindow.mainloop()
UI with button
importtkinterastkdefSayHello():print("Hellow")# Create new window# nothing will happen when you run this code# we need somethign to keep our window runningwindow=tk.Tk()# Code for GUIwindow.title("Results")window.minsize(width=500,height=70)label=tk.Label(text="Hey, you've successfully completed this test")label.pack(side="left")label.pack(anchor="nw")label.pack(padx=10,pady=10)ok_button=tk.Button(window,text="OK",command=SayHello)ok_button.pack(side="bottom")ok_button.pack(anchor="se")ok_button.pack(padx=10,pady=10)# this is require to keep window runningwindow.mainloop()
Script
importjsonimportrequests# pip install requestsfromtkinterimport*defget_fun_fact():url="https://uselessfacts.jsph.pl/api/v2/facts/random?language=en"response=requests.request("GET",url)data=json.loads(response.text)useless_fact=data['text']print(useless_fact)lbl.configure(text=useless_fact)window=Tk()window.title("Fun Fact Generator")window.geometry("800x80")btn=Button(window,text="Click Me",command=get_fun_fact)btn.pack()lbl=Label(window,text="Click the button to get random fact")lbl.pack()window.mainloop()