问题
在同一个类中,如何从方法中访问函数?
背景
你好世界!我目前正在使用 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