Loading, please wait...

A to Z Full Forms and Acronyms

My Favourite Website GUI application using tkinter | Python

Jul 20, 2020 tkinter , Webbrowser, Python, 21881 Views
this demonstrates how we create a GUI and open websites by launching the browser.

In our previous articles, we learned to create simple GUI applications using Tkinter in python. We can use other Python libraries to add more functions to the GUI applications. Through this article, we will create a GUI application using “webbrowser” library of python.

Python provides a convenient Web-browser controller. It provides a high-level interface to allow displaying Web-based documents to the user. For this, we just need to simply call the open() function from this module. This module can be used to launch a browser and opens the requested page using the default browser.

For this we are using three modules, these modules are listed

below:

  • Tkinter module
  • Webbrowser module

1.Tkinter module: Tkinter is a standard python library. Python with the Tkinter module is the fastest and easiest way to develop an object-oriented GUI application.

It provides us a variety of common GUI(graphical user interface) elements that we can use to build our user interface like buttons, menus, and various kinds of entry fields and display areas.

 Syntax of installation:

pip install tkinter

 Syntax of importing:

from tkinter import *

 2.Webbrowser module: This is used to launch the requested page using browser(default or particular if specified).

Syntax of installation:

pip install webbrowser

Syntax of importing:

import webbrowser

we have three functions that we can use with the controller.

  • open(): this helps to open the requested page using the default browser.

Syntax: webbrowser.open(url)

  • open_new(): this helps to open the requested page in a new

browser window.

Syntax: webbrowser.open_new(url)

  • open_new_tab(): this helps the open the page in a new browser tab.

Syntax: webbrowser.open_new_tab(url)

But what if we want to open the requested page using a particular browser. This we can also do specifying the name of the particular browser. This we can do by using get() function

With the controller.

Now let's discuss how we can specify the name of a particular browser. For this, we need to create an instance and then we will pass that instance as an argument in the function.

         x=webbrowser.get(“browser-name”)

         x.open(url of requested page)

         x.open_new(url of requested page)

         x=open_new_tab(url of requested page)

webbrowser module is very useful for the developers to perform some deployment to a server.

Explanation:

In this application we are using the above-mentioned libraries, We have created a Tkinter window and add 7 buttons. Whenever an On-click event will occur on the button or if the button is pressed then it functions which is bound on the button will be called and perform that function. When the button will be clicked by the user then it will launch the pages which are mention on the button.

Then users can access that page easily and the requested pages will be launched by using the default browser of our system. But if We want to specify the browser then we can do that too by using get() function. So that’s all about the application. Now let's discuss the code that we are using for the same.

CODE:

# importing webbrowser module
import webbrowser
# importing all files from tkinter module
from tkinter import *

# creating root
root = Tk()
# setting GUI title
root.title("WebBrowsers")
# setting GUI geometry
root.geometry("660x660")


# function to open linkedin in browser
def linkedin():
    webbrowser.open("www.linkedin.com")
# function to open facebook in browser
def facebook():
    webbrowser.open("www.facebook.com")
# function to open twitter in browser
def twitter():
    webbrowser.open("www.twitter.com")
# function to open youtube in browser
def youtube():
    webbrowser.open("www.youtube.com")
# function to open whatsapp web in browser
def whatsappweb():
    webbrowser.open("www.whatsappweb.com")
# function to open instagram in browser
def instagram():
    webbrowser.open("www.instagram.com")
# function to open gmail in browser
def gmail():
    webbrowser.open("www.gmail.com")

Label(root, text="WELCOME TO MY FAVOURITE \nWEBSITES", font="Helvtica 25 bold").pack()
Label(root,text="Click on the buttons to open website",font="LUCIDA").pack()
#creating button for each functions
# button to call linkedin function
mylinkedin = Button(root,text="LINKEDIN", command=linkedin,font="LUCIDA 15 bold").pack(padx=20,pady=20)
# button to call facebook function
myfacebook = Button(root, text="FACEBOOK", command=facebook,font="LUCIDA 15 bold").pack(padx=20,pady=20)
# button to call twitter  function
mytwitter = Button(root, text="TWITTER", command=twitter,font="LUCIDA 15 bold").pack(padx=20,pady=20)
# button to call youtube  function
myyoutube = Button(root, text="YOUTUBE", command=youtube,font="LUCIDA 15 bold").pack(padx=20,pady=20)
# button to call whatsappweb  function
mywhatsapp = Button(root, text="WHATSAPP WEB", command=whatsappweb,font="LUCIDA 15 bold").pack(padx=20,pady=20)
# button to call instagram  function
myinstagram = Button(root, text="INSTAGRAM", command=instagram,font="LUCIDA 15 bold").pack(padx=20,pady=20)
# button to call gmail  function
mygmail= Button(root, text="GMAIL" , command=gmail,font="LUCIDA 15 bold").pack(padx=20,pady=20)

#running the mainloop()
root.mainloop()

OUTPUT: GUI

  • when the “LinkedIn” button is clicked or pressed:
  • when the “Facebook” button is clicked or pressed:
  • when “twitter” button is clicked or pressed:

  • when the “youtube” button is clicked or pressed:
  • when “WhatsApp web” button is clicked or pressed:
  • when the “Instagram” button is clicked or pressed:
  • when the “Gmail” button is clicked or pressed:

Thank you so much for reading this article. I hope you like it too.

For any query related to python please feel free to comment. And encourage me to write more articles on python.

Till now, Keep on learning😊😊

A to Z Full Forms and Acronyms

Related Article