在python上使用全局变量作为函数的变量参数

时间:2014-04-18 08:06:26

标签: python tkinter project func globals

我试图做一个函数(在另一个函数里面是tkinter上的Toplevel()窗口)它从不同的对象获取不同的args并根据args移动它们。使用全局变量。

问题是,对于每个对象,我需要不同的全局变量,我不知道如何为每个对象放置必要的全局变量。

我试图移动用canvas.create_image()创建的图像,我已经这样做了,但是有一个特定的情况。

让我看看我是否可以更好地解释:

from tkinter import *
import os
import Threading

def CargarImagen(nombre): 
    ruta = os.path.join('Imagenes',nombre)
    imagen = PhotoImage(file=ruta)
    return imagen

Space= Tk()
Space.title("Invaders! Give Some Space!")
Space.geometry ("540x540")
Space.resizable (width=NO, height=NO)

CanvSpace= Canvas(Space, width=540, height= 540)
CanvSpace.config(cursor="dotbox")
CanvSpace.pack()

ImagenFondoInicio= CargarImagen('Pantalla Inicio.gif')
FondoCanvSpace= Label(CanvSpace,image=ImagenFondoInicio)
FondoCanvSpace.pack()

DxInvader0=1 #direction x #These are the globals I need to switch on every case
DyInvader0=0 #directtion y

DxInvader1=1
DyInvader1=0

def VentanaPlayy():
    Space.withdraw()
    VentanaPlay= Toplevel()
    VentanaPlay.title("Kill'em all!'")
    VentanaPlay.resizable(width=NO, height=NO)
    VentanaPlay.geometry("540x540")

    def MoverInvader(numInvader, Global1, Global2, Invader): #This is where I want to put the globals as variables
        global Global1, Global2 #So this would work
        x,y= CanvPlay.coords(Invader)
        if Global1+x<=Listaif1[numInvader]:
            Global1+=-0.5
            Global1= -Global1
            y+=20
        elif Global1+x>=Listaif2[numInvader]:
            Global1+=0.5
            Global1= -Global1
            y+=20
            print("Maximo Limite a la Derecha", CanvPlay.coords(Invader))
        elif y>= 500:
            print("Game Over")
        CanvPlay.coords(Invader, x+Global1, y+Global2)
        VentanaPlay.after(50,MoverInvader(numInvader, Global1, Global2, Invader))

    def ThreadMoverInvader(numeromover):
        b=Thread(target=numeromover, args=())
        b.start()

    CanvPlay= Canvas(VentanaPlay, width=540, height=540, bg="white")
    CanvPlay.config(cursor="dotbox")
    CanvPlay.place(x=-1,y=-1)

    ImagenNave= CargarImagen("Ship.gif")
    Nave= CanvPlay.create_image(260, 520, image=ImagenNave)

    ImagenHowTo= CargarImagen("HowTo.gif")
    HowTo= CanvPlay.create_image(260, 250, image=ImagenHowTo)

    ImagenNave0= CargarImagen("InvaderJuego.gif")
    Nave0= CanvPlay.create_image(21, 300, image=ImagenNave0)
    CanvPlay.after(0, ThreadMoverInvader(MoverInvader(0, DxInvader0, DyInvader0, Nave0))) #I call the function here

    ImagenNave1= CargarImagen("InvaderJuego.gif")
    Nave1= CanvPlay.create_image(51, 300, image=ImagenNave1)
    CanvPlay.after(0, ThreadMoverInvader(MoverInvader(1, DxInvader1, DyInvader1, Nave1))) #And here with their own globals and variables

    Listaif1=[20, 50] #these are the lists I'm using for every if on the function I need
    Listaif2=[490, 520]

    VentanaPlay.mainloop()
Space.mainloop()

有没有办法做到这一点?

基本上我需要做的是:

A=1
B=2

def getglobals(global1):
    global global1

并称之为:

getglobals(A)

getglobals(B)

谢谢!!

编辑:我定义了VentanaPlayy()因为我将该函数用作按钮上的命令。

顺便说一句,请原谅我糟糕的英语技巧......正如你在代码中注意到的那样......我的主要语言是西班牙语:)

来自哥斯达黎加的问候:D

EDIT2:忘记输入错误信息:

def MoverInvader(numInvader, Global1, Global2, Invader):

SyntaxError: name 'Global1' is parameter and global

0 个答案:

没有答案