1

问题

在同一个类中,如何从方法中访问函数?

背景

你好世界!我目前正在使用 Tkinter 开发一个 GUI IM 程序。要通过套接字发送消息,我需要从方法中访问函数。我的self.network方法有一个send_message()我想访问的函数。

代码

要访问它,我尝试使用以下代码:

def messageFun(self):

    //Other widgets are here//

    self.send = Button (self.messageFrame, text = "Send",
                        command = self.network(send_message()))
    self.send.grid(row = 1, column = 1, sticky = S)

self.network方法和作用send_message如下:

def network(self):

    def send_message():   
            #Send some data to the remote server
            message = self.message.get("0.0",END)

            try:
                 #set the whole string
                 s.sendall(message)
            except socket.error:
            #Send Failed
                print "Send failed"
                sys.exit()

尽管我努力调试代码,但我总是得到以下消息的奖励:

Traceback (most recent call last):
  File "D:\Python Programs\Sockets\First Project\IM Project\Server GUI InDev.py", line 178, in <module>
    app = Application(root)
  File "D:\Python Programs\Sockets\First Project\IM Project\Server GUI InDev.py", line 73, in __init__
    self.messageFun()
  File "D:\Python Programs\Sockets\First Project\IM Project\Server GUI InDev.py", line 156, in messageFun
    command = self.network(send_message()))
NameError: global name 'send_message' is not defined
4

2 回答 2

2

network(send_message())尝试触发函数是一种奇怪的语法send_message()

我建议以这种方式:

def send_message(instance):   
        #Send some data to the remote server
        message = instance.message.get("0.0",END)

        try:
             #set the whole string
             s.sendall(message)
        except socket.error:
        #Send Failed
            print "Send failed"
            sys.exit()

def network(self,what,send_message=send_message):
    if what =='send message":
        send_message(self)


self.send = Button (self.messageFrame, text = "Send",
                    command = self.network("send message"))

要么这个:

class NENE:
    def __init__(self):
        self.network.__dict__['send'] = send_message

    def send_message(self):   
        #Send some data to the remote server
        message = self.message.get("0.0",END)
        try:
             #set the whole string
             s.sendall(message)
        except socket.error:
        #Send Failed
            print "Send failed"
            sys.exit()

    def network(self):
        pass


self.send = Button (self.messageFrame, text = "Send",
                    command = self.network.send_message())

在第二个代码中,我放入passnetwork()函数,因为我不知道你的真实函数的内容,但你会用这个内容代替这个pass

我已经将这些方法放在一个类NENE中以展示它是如何工作的。因为send_message() 必须是与方法相同的实例network()的方法。
然后,在每次创建新实例时,也将其设为该方法send_message()的一个属性。 因此,完全是实例及其方法之一的属性;但没关系,除了它实现了你的目标:现在是一个可以通过属性调用的属性,它知道它适用的实例是什么。network()
send_message()send_message()network()network

原谅我糟糕的英语

于 2013-02-28T15:30:17.607 回答
0

如果send_message需要在 之外访问network,则应在 之外定义network

def send_message(self):   
    #Send some data to the remote server
    message = self.message.get("0.0",END)

    try:
        #set the whole string
        s.sendall(message)
    except socket.error:
    #Send Failed
        print "Send failed"
        sys.exit()
def network(self):
    #do network stuff...

def messageFun(self):

    //Other widgets are here//

    self.send = Button (self.messageFrame, text = "Send",
                        command = self.send_message)
    self.send.grid(row = 1, column = 1, sticky = S)

如果send_message需要访问 本地的变量network,并且network当前不返回任何内容,那么您可以定义send_messageinside network,并在最后返回它。

def network(self):
    #create the socket needed for send_message
    s = Socket()

    def send_message():   
        #Send some data to the remote server
        message = self.message.get("0.0",END)

        try:
            #set the whole string
            s.sendall(message)
        except socket.error:
        #Send Failed
            print "Send failed"
            sys.exit()

    #do other network stuff...
    return send_message

def messageFun(self):

    //Other widgets are here//

    self.send = Button (self.messageFrame, text = "Send",
                        command = self.network()
    self.send.grid(row = 1, column = 1, sticky = S)

如果网络已经有返回值,那么您可以将网络的局部变量放在类范围内以便send_message可以看到它们。

def send_message(self):   
    #Send some data to the remote server
    message = self.message.get("0.0",END)

    try:
        #set the whole string
        self.s.sendall(message)
    except socket.error:
    #Send Failed
        print "Send failed"
        sys.exit()

def network(self):
    #create the socket needed for send_message
    self.s = Socket()
    #do other network stuff

def messageFun(self):

    //Other widgets are here//

    self.send = Button (self.messageFrame, text = "Send",
                        command = self.send_message
    self.send.grid(row = 1, column = 1, sticky = S)
于 2013-02-28T15:20:01.023 回答