How do I open a website in a Tkinter window?
Last updated Dec 31, 2021It is common practice to open a website from a GUI application window. Tkinter's built-in Python webview module makes it possible for users to access HTML content directly from their Tkinter window.
Before you can use this library, you must first install it in your application. By executing the "pip install webbrowser" command, the installation will be done automatically.
Now, with the help of the create window (win title, 'URL') method, you can give the URL for your target website. This will let your Tkinter window open a new window and show the content at the URL you want.
Example
import webbrowser from tkinter import * root = Tk() root.title("Open Social Media Platforms") root.geometry("400x400") def facebook(): webbrowser.open("www.facebook.com") def twitter(): webbrowser.open("www.twitter.com") def youtube(): webbrowser.open("www.youtube.com") def whatsappweb(): webbrowser.open("www.whatsappweb.com") def gmail(): webbrowser.open("www.gmail.com") Label(root, text="SOCIAL MEDIA PLATFORMS \nWEBSITES", font="Helvtica 12 bold").pack() Label(root,text="Click Tto open social media platforms",font="LUCIDA").pack() myfacebook = Button(root, text="FACEBOOK", bg="blue", command=facebook,font="LUCIDA 12 bold").pack(padx=20,pady=20) mytwitter = Button(root, text="TWITTER", bg="skyblue", command=twitter,font="LUCIDA 12 bold").pack(padx=20,pady=20) myyoutube = Button(root, text="YOUTUBE",bg="red", command=youtube,font="LUCIDA 12 bold").pack(padx=20,pady=20) mywhatsapp = Button(root, text="WHATSAPP WEB", bg="green", command=whatsappweb,font="LUCIDA 12 bold").pack(padx=20,pady=20) mygmail= Button(root, text="GMAIL" , command=gmail,font="LUCIDA 12 bold").pack(padx=20,pady=20) root.mainloop()
|
Output
Running the code above will display a GUI window with five buttons that open social media web pages after the user clicks
![]() |
Article Contributed By :
|
|
|
|
1916 Views |