【发布时间】:2022-02-24 23:29:50
【问题描述】:
在创建窗口时,tkinter.scrolledtext 查找 SearchIP() 以插入但从不更新布局。当我对SearchIP() 方法有不同的输入时,永远不会出现。它只显示初始值。
注意:我添加了几个打印语句来观察SearchIP() 是否返回正确的结果,例如对于“www.google.com”它会打印“216.58.192.4”,但这个值永远不会显示在 ScrolledText 中。
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()
我也试过修改SearchIP()方法,去掉return语句,但后来报这个错误:
File "C:/Users/kero/Desktop/Robert_HodgesKeroles_Hakeem_secproject/secproject/newUser_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)`
修改后的搜索IP
def SearchIP():
add = str(entryVar.get())
ip_a= socket.gethostbyname(add)
【问题讨论】:
-
在
SearchIP中使用return是没用的。按钮调用SearchIP,但不能使用返回值。 -
现在代码是彩色的,所以我看到你不仅使用
SearchIP()和command,而且你需要return。 -
你应该在
SearchIP()里面使用insert()