2015-11-29 27 views
2

我在button1回調Tkinter問題。當我點擊button1我要得到所謂的SearchIP的方法,但我收到此錯誤信息:Tkinter回調

File "C:\Python27\lib\lib-tk\Tkinter.py", line 1764, in __getattr__ 
return getattr(self.tk, attr) 
AttributeError: SearchIP 

這是我的代碼嘗試至今:

from Tkinter import * 
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) 
root.button1 = Button(root,text="Search IP",command= root.SearchIP) 
root.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") 

mytext = '''\ 
Man who drive like hell, bound to get there. 
Man who run in front of car, get tired. 
Man who run behind car, get exhausted. 
Man who run in front of car, get tired. 
Man who run behind car, get exhausted. 
Man who drive like hell, bound to get there. 
Man who run in front of car, get tired. 
Man who run behind car, get exhausted. 
    ''' 
edit_space.insert('insert', mytext) 
def SearchIP(root): 
    mytext="hello" 


root.mainloop() 
+0

使用'command = SearchIP'和'def SearchIP():' – furas

回答

2

SearchIP()不是tk或部分root so use command=SearchIP

並使用def SearchIP(): - 您不必通過root作爲參數。

-

如果你需要傳遞參數SearchIP(some-argument),那麼你必須使用command=lambda:SearchIP(passed-argument)

0

使用命令= SearchIP 你必須把這個功能只是按鈕之前或在導入後..

1

SearchIP()在定義之前調用。該錯誤消息僅僅是說在解釋器不知道在該腳本中對該函數的調用更高的時候是什麼。爲了解決這個問題,將SearchIP的定義放在呼叫之上或者將呼叫移到定義之下,或者兩者兼而有之。

1

嘗試使用command=SearchIP並在調用root.button之前但定義了SearchIP之後調用root=to.Tk()。並且不要將參數傳遞給函數SearchIP,因爲您正在使用按鈕的commond參數,如果您需要傳遞參數使用lamda函數。希望這可以幫助