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

2024-09-28 01:29:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着做一个函数(在另一个函数中,它是tkinter上的Toplevel()窗口),从不同的对象中获取不同的参数,并根据这些参数移动它们。使用全局变量。你知道吗

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

我正在尝试移动用创建的图像canvas.create\图像()我已经这么做了,但是有一个具体的案例。你知道吗

让我看看我是否能更好地解释:

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)

谢谢你!!你知道吗

EDIT:我定义VentanaPlayy()是因为我将该函数用作按钮上的命令。你知道吗

顺便说一句,我的英语很差。。。正如你在代码中注意到的。。。我的主要语言是西班牙语:)

哥斯达黎加的问候:D

EDIT2:忘记输入错误消息:

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

SyntaxError: name 'Global1' is parameter and global

Tags: theimagedefcreatespacewidthgifinvader

热门问题