【问题标题】:Tkinter callbackTkinter 回调
【发布时间】:2016-03-03 06:33:01
【问题描述】:

我在button1 中遇到了 Tkinter 回调问题。当我单击 button1 时,我希望调用 SearchIP 方法,但我收到以下错误消息:

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()

【问题讨论】:

  • 使用command=SearchIPdef SearchIP():

标签: python button tkinter tk


【解决方案1】:

尝试使用command=SearchIP 并在调用root.button 之前但在调用root=to.Tk() 之后定义SearchIP。并且不要将参数传递给函数 SearchIP,因为您正在使用按钮的 commond 参数,如果您需要传递参数,请使用 lamda 函数。希望这会有所帮助

【讨论】:

    【解决方案2】:

    SearchIP() 在定义之前被调用。错误消息只是说解释器在脚本中调用该函数时不知道 SearchIP 是什么。解决这个问题,将 SearchIP 的定义向上移动到调用上方或将调用向下移动到定义下方,或两者兼而有之。

    【讨论】:

      【解决方案3】:

      使用命令=搜索IP 您必须将此函数放在按钮之前或导入之后..

      【讨论】:

        【解决方案4】:

        SearchIP() 不是tkroot 的一部分,所以使用command=SearchIP

        并使用def SearchIP(): - 您不必将root 作为参数传递。

        --

        如果您需要将参数传递给SearchIP(some-argument),那么您必须使用command=lambda:SearchIP(passed-argument)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-08-07
          • 2013-04-11
          • 2012-04-26
          • 2011-07-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多