Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

PyQt5 – GUI Application to get IP address

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article we will see how to make a simple PyQt5 application to get the IP address IP Address : An Internet Protocol address is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. An IP address serves two main functions: host or network interface identification and location addressing. Modules required and Installation: PyQt5 : PyQt5 is a Python binding of the cross-platform GUI toolkit Qt, implemented as a Python plug-in. PyQt5 is free software developed by the British firm Riverbank Computing. Requests : Requests allows you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs.

Steps for implementation : 1. Create a window 2. Create push button and set its geometry 3. Create label and set its geometry, border and alignment 4. Add action to the push button i.e when button get pressed action method should get called 5. Inside the action method with the help of requests to fetch the data 6. Filter the data to the get the IP address 7. Show IP address on the screen with the help of label

Below is the implementation 


# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import requests
import sys
class Window(QMainWindow):
    def __init__(self):
        # setting title
        self.setWindowTitle("Python ")
        # setting geometry
        self.setGeometry(100, 100, 400, 500)
        # calling method
        # showing all the widgets
    # method for widgets
    def UiComponents(self):
        # create push button to perform function
        push = QPushButton("Press", self)
        # setting geometry to the push button
        push.setGeometry(125, 100, 150, 40)
        # creating label to show the ip
        self.label = QLabel("Press button to see your IP", self)
        # setting geometry to the push button
        self.label.setGeometry(100, 200, 200, 40)
        # setting alignment to the text
        # adding border to the label
        self.label.setStyleSheet("border : 3px solid black;")
        # adding action to the push button
    # method called by push
    def get_ip(self):
        # getting data
        r = requests.get(" / ip")
        # json data with key as origin
        ip = r.json()['origin']
        # parsing the data
        parsed = ip.split(", ")[0]
        # showing the ip in label
        # setting font
        self.label.setFont(QFont('Times', 12))
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output :

My Personal Notes arrow_drop_up
Last Updated : 25 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials