'TKinter update scrolledText

On create window, tkinter.scrolledtext look up for SearchIP() to insert but never update layout. When I have different input for SearchIP() method never show up. It only shows the initial value.

Note: I added a couple of print statements to watch if SearchIP() returns the right thing, which is the case, for example for 'www.google.com' it prints '216.58.192.4', but this value never shows up in ScrolledText.

image of output

import socket
try:
    # for Python2
    import Tkinter as tk
    import ScrolledText as tkst
except ImportError:
    # for Python3
    import tkinter as tk
    import tkinter.scrolledtext as tkst

global Filename

root = tk.Tk()

frame = tk.Frame(root,width=100)
label = Label(root,text="http://")
label.grid(row=0,column=0)
entryVar = tk.StringVar()
entry = Entry(root,width=50,textvariable=entryVar)
entry.grid(row='0',column='1',columnspan=8)

def SearchIP():
  print(entryVar.get())
  add = str(entryVar.get())
  ip_a = socket.gethostbyname(add)
  print(str(ip_a))
  return str(ip_a);

button1 = Button(root, text="Search IP", command=SearchIP)
button1.grid(row=1, column=0)
button2 = Button(root, text="DNS Recon")
button2.grid(row=1, column=1)
button3 = Button(root, text="Port Scanner")
button3.grid(row=1, column=2)
button4 = Button(root, text="Web Crawl")
button4.grid(row=1, column=3)
button5 = Button(root, text="Email Gathering")
button5.grid(row=1, column=4)
frame.grid(row=2, column=0, columnspan=30, rowspan=30)
edit_space = tkst.ScrolledText(
    master = frame,
    wrap   = 'word',  # wrap text at full words only
    width  = 45,      # characters
    height = 10,      # text lines
     # background color of edit area
)
# the padx/pady space will form a frame
edit_space.pack(fill='both', expand=True, padx=8, pady=8)
root.title("E-Z Security Audting")
root.resizable(True, True)
edit_space.insert('insert', SearchIP())

root.mainloop()

I also tried to modify the SearchIP() method and remove the return statement, but then I got this error:

  File "C:/Users/kero/Desktop/Robert_HodgesKeroles_Hakeem_secproject/secproject/new‌​User_Interface.py", line 50, in <module>
    edit_space.insert('insert', SearchIP())
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2986, in insert
    self.tk.call((self._w, 'insert', index, chars) + args)`

SearchIP after modification

def  SearchIP():
    add = str(entryVar.get())
    ip_a= socket.gethostbyname(add)


Solution 1:[1]

You have to use insert() inside SearchIP()

import socket

try:
  # for Python2
  import Tkinter as tk
  import ScrolledText as tkst
except ImportError:
  # for Python3
  import tkinter as tk
  import tkinter.scrolledtext as tkst

# --- functions ---

def SearchIP():
    add = entryVar.get()
    ip_a = socket.gethostbyname(add)
    edit_space.insert('insert', ip_a + "\n")

# --- main ---

root = tk.Tk()

root.title("E-Z Security Audting")
root.resizable(True, True)

frame = tk.Frame(root, width=100)
frame.grid(row=2, column=0, columnspan=30, rowspan=30)

label = tk.Label(root, text="http://")
label.grid(row=0, column=0)

entryVar = tk.StringVar()
entry = tk.Entry(root, width=50, textvariable=entryVar)
entry.grid(row=0, column=1, columnspan=8)

button1 = tk.Button(root, text="Search IP", command=SearchIP)
button1.grid(row=1, column=0)

button2 = tk.Button(root, text="DNS Recon")
button2.grid(row=1, column=1)

button3 = tk.Button(root, text="Port Scanner")
button3.grid(row=1, column=2)

button4 = tk.Button(root, text="Web Crawl")
button4.grid(row=1, column=3)

button5 = tk.Button(root, text="Email Gathering")
button5.grid(row=1, column=4)

edit_space = tkst.ScrolledText(
    master=frame,
    wrap='word',  # wrap text at full words only
    width=45,      # characters
    height=10,      # text lines
     # background color of edit area
)
edit_space.pack(fill='both', expand=True, padx=8, pady=8)

root.mainloop()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 furas