Python Forum
[Tkinter] Display IP address in listbox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Display IP address in listbox
#1
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()
Reply
#2
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.
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Display and update the label text which display the serial value jenkins43 5 5,543 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 1,994 Jan-05-2019, 01:48 PM
Last Post: wuf
  [Tkinter] THow to search a string in a file and display it in listbox by Tkinter mosavit 2 4,053 Jun-21-2017, 08:02 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020