Nerdegutta's logo

nerdegutta.no

Python: Get your public IP adress

06.07.25

Programming

This Python app get your public IP address and displays it in a small window. Tkinter is used as the GUI.

You need Python3 installed, and TKinter. To install TKinter, use this command:

# apt install python3-tk 
The app:
import tkinter as tk
import urllib.request
import threading
import time

UPDATE_INTERVAL = 24 * 60 * 60  # 24 hours in seconds

def get_public_ip():
    try:
        with urllib.request.urlopen('https://api.ipify.org') as response:
            return response.read().decode('utf-8')
    except Exception as e:
        return f"Error: {e}"

class IPApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Public IP Viewer")
        self.root.geometry("300x120")

        self.label_title = tk.Label(root, text="Your Public IP:", font=("Helvetica", 14))
        self.label_title.pack(pady=10)

        self.ip_label = tk.Label(root, text="Loading...", font=("Helvetica", 12), fg="blue")
        self.ip_label.pack()

        self.update_ip()
        threading.Thread(target=self.schedule_ip_update, daemon=True).start()

    def update_ip(self):
        ip = get_public_ip()
        self.ip_label.config(text=ip)

    def schedule_ip_update(self):
        while True:
            time.sleep(UPDATE_INTERVAL)
            self.root.after(0, self.update_ip)

if __name__ == "__main__":
    root = tk.Tk()
    app = IPApp(root)
    root.mainloop()
Enjoy!