-2

当我想访问 int(COORDINATES 2 ) 时,这不起作用。我该如何解决?我正在尝试访问现在已成为全球范围的 COODINATES

import socket
import os,sys
import time
from threading import Thread

HOST = '59.191.193.59'
PORT = 5555

COORDINATES = []

def connect():   
    globals()['client_socket'] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((HOST,PORT))

def update_coordinates():
    connect()
    screen_width = 0
    screen_height = 0
    while True:
        try:
            client_socket.send("loc\n")
            data = client_socket.recv(8192)
        except:
            connect();
            continue;

        globals()['COORDINATES'] = data.split()

        if(not(COORDINATES[-1] == "eom" and COORDINATES[0] == "start")):
            continue

        if (screen_width != int(COORDINATES[2])):
        screen_width = int(COORDINATES[2])
                screen_height = int(COORDINATES[3])   

Thread(target=update_coordinates).start()

while True:
    print 'ball x' , int(COORDINATES[2])

这是我得到的错误在此处输入图像描述 在此处输入图像描述

如果我将代码更改为:

    import socket
import os,sys
import time
from threading import Thread

HOST = '59.191.193.59'
PORT = 5555

COORDINATES = []

def connect():   
    globals()['client_socket'] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((HOST,PORT))

def update_coordinates():
    connect()
    screen_width = 0
    screen_height = 0
    while True:
        try:
            client_socket.send("loc\n")
            data = client_socket.recv(8192)
        except:
            connect();
            continue;

        globals()['COORDINATES'] = data.split()

        if(not(COORDINATES[-1] == "eom" and COORDINATES[0] == "start")):
            continue

        if (screen_width != int(COORDINATES[2])):
        screen_width = int(COORDINATES[2])
                screen_height = int(COORDINATES[3]) 

    print int(COORDINATES[8])  

Thread(target=update_coordinates).start()

这就是我得到的:

在此处输入图像描述

4

1 回答 1

1

这一行:

Thread(target=update_coordinates()).start()

应该读:

Thread(target=update_coordinates).start()

您提供了函数的结果作为目标的值,而不是提供函数本身。

于 2012-10-05T01:28:47.973 回答