- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display the Host Name and IP Address on a Tkinter Window
To obtain a user's IP address, we can use Python's native networking interface, socket. First of all, we need to query the device's host name and then get its associated IP address.
In this example, we will use the socket library to get the host name and IP address and print the details on two labels.
Steps −
Import the tkinter library and create an instance of tkinter frame.
Set the size of the frame using geometry method.
Next, use the gethostname() method of socket library to get the host name and store it in a variable "hostname".
Then use the gethostbyname() method and pass the hostname in it to get the IP address.
Create two labels to display the hostname and IP address on the window.
Finally, run the mainloop of the application window.
Example
# Import the tkinter library from tkinter import * import socket # Create an instance of tkinter frame root = Tk() # Size of the window root.geometry("700x300") # hostname of the socket hostname = socket.gethostname() # IP address of the hostname ip_address = socket.gethostbyname(hostname) label1 = Label(root, text="The Host Name is: " + hostname, font = "Calibri, 20") label1.pack(pady=50) label2 = Label(root, text="The IP Address is: " + ip_address, font = "Calibri, 20") label2.pack(pady=20) root.mainloop()
Output
It will produce the following output −
- Related Articles
- Program to display hostname and IP address C
- Python program to Display Hostname and IP address?
- C# program to Display Hostname and IP address
- C Program to display hostname and IP address
- Java program to display Hostname and IP address
- How to get a Docker container's IP address from the host?
- Difference between Static IP Address and Dynamic IP Address
- Difference between IP Address and MAC Address
- Difference between MAC Address and IP Address
- How to display the IP Address of the Machine using C#?
- Tkinter-How to get the current date to display in a tkinter window?
- Removing the TK icon on a Tkinter window
- Difference between URL and IP Address
- Alias/Secondary IP Address
- How to get the current date to display in a Tkinter window?
