如何从方法在Python访问功能(How to Access Functions from Meth

2019-09-26 11:34发布

在同一个班,如何从一个方法中访问的功能?

背景

你好,世界! 我目前正在使用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

Answer 1:

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属性,它知道什么是它的工作原理的实例。

请原谅我的英语很差



Answer 2:

如果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需要访问本地变量networknetwork目前并不返回任何东西,那么你可以定义send_message内部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)


文章来源: How to Access Functions from Methods in Python