Wednesday, November 20, 2024
Google search engine
HomeUncategorisedHow to open a website in a Tkinter window?

How to open a website in a Tkinter window?

In this article, we are going to see how we can open a website in the Tkinter window. We can open a website in Tkinter using webview. This library allows us to view the HTML content in its GUI window.  

Syntax to install Tkinter and webview using the below commands.

pip install tk
pip install pywebview

Method 1: Using  webview.create_window() and webview.start() function

In this method, we are use webview.create_window() and webview.start() function to open website in Tkinter. create_window() function is create window for website and start() function display this website on screen. Follow the below steps to open a website in Tkinter using this method.

  • Import Tkinter and webview libraries.
  • define an instance of Tkinter.
  • Set the size of your window.
  • Call webview.create_window() function.

Example:

Python3




# Import tkinter and webview libraries
from tkinter import *
import webview
  
# define an instance of tkinter
tk = Tk()
  
#  size of the window where we show our website
tk.geometry("800x450")
  
# Open website
webview.create_window('Geeks for Geeks', 'https://geeksforgeeks.org')
webview.start()


Output:

Method 2: Using webbrowser.open() function

In this method. we use webbrowser.open() function. This function opens the requested page using the default browser. Follow the below steps to open a website in Tkinter using this method.

  • Import Tkinter and webview libraries.
  • define an instance of Tkinter.
  • Set the size of your window.
  • Call webbrowser.open() function.

Example:

Python3




# import required library
import webbrowser
from tkinter import *
  
# creating root
root = Tk()
  
# setting GUI title
root.title("WebBrowsers")
  
# setting GUI geometry
root.geometry("660x660")
  
# call webbrowser.open() function.
webbrowser.open("www.instagram.com")


Output:

RELATED ARTICLES

Most Popular

Recent Comments