命令通过define

时间:2016-06-06 00:48:58

标签: python function pygame

我正在使用pygame在python中创建一个程序,这样当你按下屏幕上的按钮时,它会执行一个命令。我通过一个定义来做这个,但我似乎无法使用该命令。这是我的代码(顺便说一句,我使用的是python 2.7.11)

server {
    listen   80;
    charset utf-8;
    client_max_body_size 100M;
    ssl on;
    listen 443 ssl;


    ssl_certificate /etc/nginx/ssl/yoursite_com/ssl-bundle.crt;
    ssl_certificate_key /etc/nginx/ssl/yoursite_com/cert.key;



    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";

    access_log /var/www/vhosts/yoursite.com/logs/access_log;
    error_log  /var/www/vhosts/yoursite.com/logs/error_log;
    server_name yousite.com www.yoursite.com;

    root /var/www/vhosts/yoursite.com/yourapp/;
    add_header Strict-Transport-Security max-age=31536000;


    location / {
          .... your settings here
    }
    location /media {
        alias /var/www/vhosts/yoursite.com/yourapp/media;
    }

    location /static {
        alias /var/www/vhosts/yoursite.com/yourapp/static;
    }
    if ($scheme = http) {
        return 301 https://$server_name$request_uri;
    }

}

正如你所看到的,我正在调用我点击按钮时运行的变量命令的另一个定义。当我运行它并单击按钮时,没有任何错误,没有任何反应。有人可以告诉我如何正确地做到这一点?我在谷歌搜索但我没有任何相关。

1 个答案:

答案 0 :(得分:0)

当您将msg()放入参数列表中button时,您不会将该函数作为参数传递,而是立即调用它并传递其返回值(None case)作为论点。摆脱括号,你将传递对函数的引用:

button(60,60,100,100,red,black,green,msg)

现在您只需要修复从button函数内部调用命令的操作。而不只是命名它command,通过在末尾添加括号来调用它:

        if (event.type == MOUSEBUTTONDOWN):
            pygame.draw.rect(screen, color3, [posX, posY,length,width])
            pygame.display.flip()
            command()