Python Forum
[Tkinter] Display IP address in listbox - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Display IP address in listbox (/thread-30448.html)



Display IP address in listbox - dominicrsa - Oct-21-2020

Hi,

I'm trying to display various Windows settings in a simple Python GUI, starting with the current IP
(The next step would be to add a new button to change the IP etc...)

The IP currently gets displayed in the terminal window rather than in the listbox as desired, just not sure
how to resolve this? Full code is below, any help on this will be hugely appreciated.

from tkinter import *
import socket

def displayIP():
list1.delete(0,END)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8",80))
ADDR = (print(s.getsockname()[0]))
list1.insert(END,ADDR)

window=Tk()

window.wm_title("Quick Configurator")

list1=Listbox(window, height=6,width=35)
list1.grid(row=2,column=0,rowspan=6,columnspan=2)

sb1=Scrollbar(window)
sb1.grid(row=14,column=2,rowspan=10)

list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)

b1=Button(window,text="Current IP", width=16,command=displayIP)
b1.grid(row=2,column=3)

window.mainloop()


RE: Display IP address in listbox - deanhystad - Oct-21-2020

It is printed to the console because you are calling print. And print does not return the socket name, it returns None. ADDR = (print(s.getsockname()[0])) sets ADDR = None, so you are adding None to your listbox, not the socket name.


RE: Display IP address in listbox - dominicrsa - Oct-21-2020

(Oct-21-2020, 12:43 PM)deanhystad Wrote: It is printed to the console because you are calling print. And print does not return the socket name, it returns None. ADDR = (print(s.getsockname()[0])) sets ADDR = None, so you are adding None to your listbox, not the socket name.

Thank you! Managed to resolve the issue (Day 2 of Python so a lot to learn!). Appreciate your help