更新Tkinter标签

时间:2013-09-02 15:54:38

标签: python tkinter label

我使用Python 3,我对编程很新。我写了一个代码,它应该显示一个窗口,你可以在这个窗口中移动两个行星并看到它们之间的引力。 除了应该显示力量的标签外,一切都有效。经过多次尝试和搜索后,我无法找到每次移动行星时如何更新它。

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

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()

1 个答案:

答案 0 :(得分:2)

好的,有一些事情需要做。首先,制作这样的标签:

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)

move函数的末尾,以便每次向任何方向移动时,它都会更新标签。

第三,不要在grav中返回gravitation,而是将其放在最后:

lbl["text"] = grav

现在,每次调用gravitation时,标签都会更新。

总而言之,这应该做你想做的事:

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函数将覆盖此内容。希望这有帮助!