Programmatically opening URLs in a web browser in Python (tkinter)


Python has a rich library of extensions and modules which are used for multiple purposes. To work with web-based content, Python provides a webbrowser module. The module creates an environment which enables the user to display web-based content in the application. To work with webbrowser, you have to make sure that it is installed in your local machine.

import webbrowser

If the module is not available in your environment, then you can install it using the following command −

pip install webbrowser

Example

Using the webbrowser module in our program, we will open an URL in our web browser. To open the URL in a default browser, we can use the open() function in the module.

# Import the required libraries
import webbrowser

# Add a URL to open in a new window
url= 'https://www.tutorialspoint.com/'

# Open the URL in a new Tab
webbrowser.open_new_tab(url)

Output

Running the above code will open the given URL in a new window of the default browser.

Updated on: 08-Jun-2021

451 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements