gpt4 book ai didi

python - 如何在命令函数调用中避免全局变量

转载 作者:行者123 更新时间:2023-12-01 05:37:22 25 4
gpt4 key购买 nike

我仍在研究围绕两个行星移动并显示它们之间的引力的代码。我试图让它更容易使用,只显示两个按钮,允许选择你想要移动的星球。然后您可以在 Canvas 中单击,所选行星将移动到您单击的位置。

该程序可以工作,但我想知道是否有更好的方法来编写它,然后使用带有全局语句的 chngB 和 chngO 函数。我仍然不敢相信,在 Python 中,在将函数分配给按钮的 command 参数时,您被迫使用不带参数的函数。

基本上我想知道是否可以编写类似 command = (a=1)(我知道这行不通,但你明白了。)另外,可能还有另一种方法可以做到这一点,而不是必须使用变量来知道选择了哪个行星(最后按下了哪个按钮)。

我使用Python 3。

from tkinter import *
import math

x, y = 135, 135
a = 0

def gravitation (obj1,obj2):#showing gravitational force between planets
a, b, c, d = can.coords (obj1)
e, f, g, h = can.coords (obj2)
dist = math.sqrt ((((a+c)/2)-((e+g)/2))**2+(((b+d)/2)-((f+h)/2))**2)
if dist != 0:
grav = 6.67384/dist
else:
grav = "Infinite"
str(grav)
return grav

def chngB ():#Telling Blue planet is selected
global a
a = 1

def chngO ():#Telling Orange planet is selected
global a
a = 0

def updt ():#Updating gravitation label
lbl.configure (text = gravitation(oval1, oval2))


def moveBlue (event):#Placing blue planet where mouse click on canv
coo = [event.x-15, event.y-15, event.x+15, event.y+15]
can.coords(oval1, *coo)
updt()

def moveOrange (event):#Placing orange planet where mouse click on canv
coo = [event.x-15, event.y-15, event.x+15, event.y+15]
can.coords(oval2, *coo)
updt()

def choice (event):#Function binded to can, move the selected planet (blue = 1, prange = 0)
if a == 0:
moveOrange(event)
else :
moveBlue(event)

##########MAIN############

wind = Tk() # Window and canvas
wind.title ("Move Da Ball")
can = Canvas (wind, width = 300, height = 300, bg = "light blue")
can.grid(row=0, column=0, sticky =W, padx = 5, pady = 5, rowspan =3)
can.bind ("<Button-1>", choice)
Button(wind, text = 'Quit', command=wind.destroy).grid(row=2, column=1, sticky =W, padx = 5, pady = 5)

oval1 = can.create_oval(x,y,x+30,y+30,width=2,fill='blue') #Planet 1 moving etc
buttonBlue = Button(wind, text = 'Blue Planet', command = chngB)
buttonBlue.grid(row=1, column=1, sticky =W, padx = 5, pady = 5)

oval2 = can.create_oval(x+50,y+50,x+80,y+80,width=2,fill='orange') #Planet 2 moving etc
buttonOrange = Button(wind, text = 'Orange Planet', command = chngO)
buttonOrange.grid(row=0, column=1, sticky =W, padx = 5, pady = 5)

lbl = Label(wind, bg = 'white')#label
lbl.grid(row=4, column=1, sticky =W, padx = 5, pady = 5, columnspan = 3)
gravitation (oval1, oval2)

wind.mainloop()

最佳答案

通常,如果您想要一个按钮在 N 个值之一之间切换变量,您可以使用一组单选按钮。当您使用单选按钮时,您可以将它与一个变量关联起来,这样每当您单击该按钮时,就会自动选择该变量。

例如:

planet = IntVar()
planet.set(0)

buttonBlue = Radiobutton(wind, text="Blue Planet", variable=planet, value=1)
buttonOrange = Radiobutton(wind, text="Orange Planet", variable=planet, value=0)
...
def choice (event):#Function binded to can, move the selected planet (blue = 1, prange = 0)
if planet.get() == 0:
moveOrange(event)
else :
moveBlue(event)

如果您确实想使用常规按钮,可以使用单个回调而不是两个回调来实现,然后使用 lambda 或 functools.partial 传入新值。

例如:

buttonBlue = Button(wind, text = 'Blue Planet', command = lambda: change(1))
buttonOrange = Button(wind, text = 'Blue Planet', command = lambda: change(0))
def change(newValue):
global a
a = newValue

关于python - 如何在命令函数调用中避免全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18596271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com