Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更新Tkinter标签_Python_Tkinter_Label - Fatal编程技术网

Python 更新Tkinter标签

Python 更新Tkinter标签,python,tkinter,label,Python,Tkinter,Label,我使用Python3,我对编程非常陌生。我写了一个代码,它应该显示一个窗口,在这个窗口中你可以绕着两颗行星移动,并看到它们之间的引力。 除了用来显示原力的标签外,一切都正常。经过多次尝试和搜索,我就是不知道如何在每次行星移动时更新它 我想我的问题应该在最后一行: lbl = Label(wind, bg = 'white', text = gravitation(oval1, oval2)).pack(padx=5, pady=5) 我曾尝试使用StringVar和textvariable参数

我使用Python3,我对编程非常陌生。我写了一个代码,它应该显示一个窗口,在这个窗口中你可以绕着两颗行星移动,并看到它们之间的引力。 除了用来显示原力的标签外,一切都正常。经过多次尝试和搜索,我就是不知道如何在每次行星移动时更新它

我想我的问题应该在最后一行:

lbl = Label(wind, bg = 'white', text = gravitation(oval1, oval2)).pack(padx=5, pady=5)
我曾尝试使用StringVar和textvariable参数,但我没有真正了解它的概念

这是我的密码。我想答案很简单,但我没有经验

from tkinter import *
import math

x, y = 135, 135
gravitation = 0

def gravitation (obj1,obj2):
    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)
    grav = 6.67384/dist
    return grav

def move (ov, lr, tb): # function to move the ball
    coo = can.coords(ov)
    coo[0] = coo[0] + lr
    coo[1] = coo[1] + tb
    coo[2] = coo[0]+30
    coo[3] = coo[1]+30
    can.coords(ov, *coo)


def moveLeft ():
    move(oval1, -10, 0)

def moveRight ():
    move(oval1, 10, 0)

def moveTop ():
    move(oval1, 0, -10)

def moveBottom ():
    move(oval1, 0, 10)

def moveLeft2 ():
    move(oval2, -10, 0)

def moveRight2 ():
    move(oval2, 10, 0)

def moveTop2 ():
    move(oval2, 0, -10)

def moveBottom2 ():
    move(oval2, 0, 10)


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

wind = Tk() # Window and canvas
wind.title ("Move Da Ball")
can = Canvas (wind, width = 300, height = 300, bg = "light blue")
can.pack (side = LEFT,padx = 5, pady = 5)
Button(wind, text = 'Quit', command=wind.destroy).pack(padx = 5, pady = 5)

oval1 = can.create_oval(x,y,x+30,y+30,width=2,fill='orange') #Planet 1 moving etc
Button(wind, text = 'Left', command=moveLeft).pack(padx = 5, pady = 5)
Button(wind, text = 'Right', command=moveRight).pack(padx = 5, pady = 5)
Button(wind, text = 'Top', command=moveTop).pack(padx = 5, pady = 5)
Button(wind, text = 'Bottom', command=moveBottom).pack(padx = 5, pady = 5)

oval2 = can.create_oval(x+50,y+50,x+80,y+80,width=2,fill='orange') #Planet 2 moving etc
Button(wind, text = 'Left', command=moveLeft2).pack(padx = 5, pady = 5)
Button(wind, text = 'Right', command=moveRight2).pack(padx = 5, pady = 5)
Button(wind, text = 'Top', command=moveTop2).pack(padx = 5, pady = 5)
Button(wind, text = 'Bottom', command=moveBottom2).pack(padx = 5, pady = 5)


lbl = Label(wind, bg = 'white', text = gravitation(oval1, oval2)).pack(padx=5, pady=5)

wind.mainloop()

好的,有几件事需要做。首先,让您的标签如下所示:

lbl = Label(wind, bg = 'white', text = gravitation(oval1, oval2))
lbl.pack(padx=5, pady=5)
# Put this on its own line so that there are no errors since 'lbl' isn't defined yet
gravitation(oval1, oval2)
这将使
lbl
像应该的那样引用标签,而不是
pack
,后者返回
None

其次,您需要将以下内容:

gravitation(oval1, oval2)
移动
功能的末尾,这样每次在任何方向上移动时,它都会更新标签

第三,不要在
引力
中返回
重力
,而是将其放在末尾:

lbl["text"] = grav
现在,每次调用
引力
,标签都会更新

总之,这应该满足您的要求:

from tkinter import *
import math

x, y = 135, 135

def gravitation (obj1,obj2):
    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)
    grav = 6.67384/dist
    ##################
    lbl["text"] = grav
    ##################

def move (ov, lr, tb):
    coo = can.coords(ov)
    coo[0] = coo[0] + lr
    coo[1] = coo[1] + tb
    coo[2] = coo[0]+30
    coo[3] = coo[1]+30
    can.coords(ov, *coo)
    ########################
    gravitation(oval1, oval2)
    ########################


def moveLeft ():
    move(oval1, -10, 0)

def moveRight ():
    move(oval1, 10, 0)


def moveTop ():
    move(oval1, 0, -10)

def moveBottom ():
    move(oval1, 0, 10)

def moveLeft2 ():
    move(oval2, -10, 0)

def moveRight2 ():
    move(oval2, 10, 0)

def moveTop2 ():
    move(oval2, 0, -10)

def moveBottom2 ():
    move(oval2, 0, 10)



wind = Tk()
wind.title ("Move Da Ball")
can = Canvas (wind, width = 300, height = 300, bg = "light blue")
can.pack (side = LEFT,padx = 5, pady = 5)
Button(wind, text = 'Quit', command=wind.destroy).pack(padx = 5, pady = 5)

oval1 = can.create_oval(x,y,x+30,y+30,width=2,fill='orange') #Planet 1 moving etc
Button(wind, text = 'Left', command=moveLeft).pack(padx = 5, pady = 5)
Button(wind, text = 'Right', command=moveRight).pack(padx = 5, pady = 5)
Button(wind, text = 'Top', command=moveTop).pack(padx = 5, pady = 5)
Button(wind, text = 'Bottom', command=moveBottom).pack(padx = 5, pady = 5)

oval2 = can.create_oval(x+50,y+50,x+80,y+80,width=2,fill='orange') #Planet 2 moving etc
Button(wind, text = 'Left', command=moveLeft2).pack(padx = 5, pady = 5)
Button(wind, text = 'Right', command=moveRight2).pack(padx = 5, pady = 5)
Button(wind, text = 'Top', command=moveTop2).pack(padx = 5, pady = 5)
Button(wind, text = 'Bottom', command=moveBottom2).pack(padx = 5, pady = 5)

###############################
lbl = Label(wind, bg = 'white')
lbl.pack(padx=5, pady=5)
gravitation(oval1, oval2)
##############################

wind.mainloop()

我把我改变的东西都放在评论框里。另外,我在一开始就删除了
gravitation=0
,因为定义
gravitation
函数将覆盖它。希望这有帮助

嘿。谢谢你的帮助。事实上,我希望有其他的方法来做这件事,但这一个有效。我会再等一会儿,看看有没有人有什么想法,否则我会把它作为答案。@Pierreconom:你不喜欢这个解决方案的什么地方?我在编程方面不是很有经验,所以我可能不知道说什么我不喜欢这个解决方案,因为我自己也没有任何线索。但如果我要提到的话,我需要在每一个移动函数中调用引力函数。如果可以的话,我更愿意让我的代码更通用。我正在做其他的事情,一个几乎可以用的东西,除了一个小问题。我正在写一个新问题,你可以看一看。@PierreOcinom-我只是想到了一些事情(不知道为什么我之前没有想到)。在阅读了您的上述评论之后,我开始思考一种方法,在每次移动函数中不调用
重力
就可以完成任务。我的解决方案是在
move
函数中调用
gravitation
。请参阅我的编辑。这样就不需要一直调用
引力
。哈哈哈。事实上,这是如此容易,甚至我应该看到它。谢谢。